Misha AM
Misha AM

Reputation: 137

How to drop columns with empty headers in Pandas?

If I have a DF:

Name1 Name2 NUll Name3 NULL Name4
abc   abc        null       abc
abc   abc        null       abc
abc   abc        null       abc
abc   abc        null       abc

Can I use dropna, to keep Name3 as a column with all empty values? Yet still drop both Null columns. Thank you

Upvotes: 2

Views: 10127

Answers (2)

dcsan
dcsan

Reputation: 12325

I was importing some data from a google sheet which had empty column names, this did the required for me:

    # drop those with empty column names
    self.df.drop([""], axis=1, inplace=True)

Upvotes: 1

wflynny
wflynny

Reputation: 18551

What about using DataFrame.drop?

In [3]: df = pd.read_clipboard()
Out[3]: 
  Name1 Name2  NUll Name3  NULL  Name4
0   abc   abc        null          abc
1   abc   abc        null          abc
2   abc   abc        null          abc
3   abc   abc        null          abc

In [4]: df.drop(["NUll", "NULL"], axis=1)
Out[4]: 
  Name1 Name2 Name3  Name4
0   abc   abc  null    abc
1   abc   abc  null    abc
2   abc   abc  null    abc
3   abc   abc  null    abc

Upvotes: 2

Related Questions