diogenes
diogenes

Reputation: 2149

Pandas delete Column where Header is NAN

How can I drop all column with a NaN value from dataframes?

      count       bal  NaN        NaN      sum           NaN

0   10.534709  254.38   0  215490.85  -7118.52     2013-07-31
1    0.000000  135.39   0  227822.73 -10034.44     2013-08-31
2    0.000000  143.58   0  247432.23   -107.51     2013-09-30

I have tried various versions of this:

df.drop ( '', axis = 1 )

Thank you.

Upvotes: 1

Views: 1392

Answers (3)

Dishin H Goyani
Dishin H Goyani

Reputation: 7723

You can use dropna

df = df[df.columns.dropna()]
df
       count     bal       sum
0  10.534709  254.38  -7118.52
1   0.000000  135.39 -10034.44
2   0.000000  143.58   -107.51

Upvotes: 3

Quang Hoang
Quang Hoang

Reputation: 150825

You can do:

df = df.loc[:, df.columns.notnull()]

Upvotes: 1

diogenes
diogenes

Reputation: 2149

might not be best solution but it worked.

I found a way to rename the NaN columns with the .fillna - here I just renamed them to 'z'.

after that I had a way to drop the columns.

        df.columns = df.columns.fillna ( 'z' )
        df.drop ( ['comments', 'z'], axis = 1, inplace=True )

always open to a better idea. cheers.

Upvotes: 0

Related Questions