Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Row if NaN in First Five Columns

I have a pandas dataframe with dimensions 89 rows by 13 columns. I want to remove an entire row if NaN appears within the first five columns. Here is an example.

LotName     C15   C16  C17  C18  C19 Spots15 Spots16 ...
Cherry St   439   464  555  239  420     101     101 ...
Springhurst NaN   NaN  NaN  NaN  NaN      12      12
Barton Lot   34    24   43   45   39      10       9 ...

In the above example, I would want to remove the Springhurst observation, as it contains NaN within the first five columns. How would I be able to do this in Python?

like image 421
325 Avatar asked Dec 17 '22 12:12

325


1 Answers

If you want to do a strict check of Nan in all rows for first 5 columns:

df.iloc[:, :5].dropna(how='all')

Explanation:

df.iloc[:, :5] : select all rows and first 5 columns

.dropna(how='all') : check if all values in a row are NaN

If you want to check for Nan in any of the 5 columns:

df.iloc[:, :5].dropna(how='any')

For assigning it back to original df, you can do this:

In [2107]: ix = df.iloc[:, :5].dropna(how='all').index.tolist()

In [2110]: df = df.loc[ix]

In [2111]: df
Out[2111]: 
       LotName    C15    C16    C17    C18  C19  Spots15  Spots16
Cherry      St  439.0  464.0  555.0  239.0  420      101    101.0
Barton     Lot   34.0   24.0   43.0   45.0   39       10      9.0
like image 128
Mayank Porwal Avatar answered Dec 22 '22 00:12

Mayank Porwal