0

How do I check if there is a column filled with only 'None' values? How do I drop that column?

So something similar to:

If column in dataframe is None:
    drop column

Here's a dummy data example where the solution should identify the 5th column as empty and drop that column:

import pandas as pd
import numpy as np

# dictionary of lists
dict = {'First Score':[100, np.nan, np.nan, 95],
        'Second Score': [30, np.nan, 45, 56],
        'Third Score':[52, np.nan, 80, 98],
        'Fourth Score':[60, 67, 68, 65],
        'Fifth Score':[ np.nan,  np.nan,  np.nan,  np.nan]
        }

# creating a dataframe from dictionary
df = pd.DataFrame(dict)
df = df.where(pd.notnull(df), None)
Spatial Digger
  • 1,883
  • 1
  • 19
  • 37

1 Answers1

2

Try this:

df_out = df.dropna(how='all', axis=1)

Output:

  First Score Second Score Third Score  Fourth Score
0       100.0         30.0        52.0            60
1        None         None        None            67
2        None         45.0        80.0            68
3        95.0         56.0        98.0            65

Per docs, pd.DataFrame.dropna has parameter 'how' which we can set to 'all'.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187