Reputation: 129
I have a dataframes with many rows, and some values are NaNs.
For example -
index col1 col2 col3
0 1.0 NaN 3.0
1 NaN 4.0 NaN
3 1.0 5.0 NaN
I would like to filter the DF and return only the rows with 2+ values.
The number should be configurable.
The resulted DF will be -
index col1 col2 col3
0 1.0 NaN 3.0
3 1.0 5.0 NaN
Any idea how can I achieve this result? I've tried creating new column but it doesn't seem the right way.
Thanks!
Code to create the DF:
d = {'col1': [1, None, 1], 'col2': [None, 4, 5], 'col3': [3, None, None]}
df = pd.DataFrame(data=d)
df
Upvotes: 1
Views: 67
Reputation: 75
You can delete the 2nd row by using the drop()
method.
ax = df.drop([1])
print(ax)
Upvotes: -1
Reputation: 13821
You can use dropna()
set the threshold to be 2 thresh=2
, and perform operation along the rows axis=0
:
res = df.dropna(thresh=2,axis=0)
res
col1 col2 col3
0 1.00 NaN 3.00
2 1.00 5.00 NaN
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html
Upvotes: 2