pitosalas
pitosalas

Reputation: 10912

Delete a column that has all Nan's in Pandas

 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

Answers (4)

Sazzad
Sazzad

Reputation: 21

This worked for me

x.dropna(axis=1, how='all', inplace=True)

Upvotes: 0

zipa
zipa

Reputation: 27899

You can use loc:

x.loc[:, ~x.isnull().all()]

Upvotes: 1

BENY
BENY

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

MaxU - stand with Ukraine
MaxU - stand with Ukraine

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

Related Questions