Reputation: 10912
x = pd.DataFrame({'x':[np.nan, 22, 11, np.nan, np.nan],
'letters':['a', 'a', 'b', 'b', 'b'],
'Nan1': [np.nan, np.nan, np.nan, np.nan, np.nan],
'bools': [True, True, False, True, False],
'Nan2': [np.nan, np.nan, np.nan, np.nan, np.nan],
'y': [100,200,11,333, 70]})
I'd like to learn the best way to delete all columns from this DataFrame who have all NaNs. In this case, it would delete columns Nan1 and Nan2.
I have a feeling there's a good way to do this!
Upvotes: 1
Views: 6668
Reputation: 323396
Using dropna
with thresh
(thresh : int, default None
int value : require that many non-NA values)
x.dropna(1,thresh=1)
Out[721]:
bools letters x y
0 True a NaN 100
1 True a 22.0 200
2 False b 11.0 11
3 True b NaN 333
4 False b NaN 70
Upvotes: 2
Reputation: 210982
AFAIK DataFrame.dropna() is the most idiomatic way to do that:
In [17]: x = x.dropna(how='all', axis=1)
In [18]: x
Out[18]:
bools letters x y
0 True a NaN 100
1 True a 22.0 200
2 False b 11.0 11
3 True b NaN 333
4 False b NaN 70
Upvotes: 10