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?
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')
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With