I have a dataframe that holds a number of NoneType values and I would like to drop all columns where all the row values AND the header is None. I am struggling to find a way to do this. In the MWE below I have managed to either drop all columns where all the rows are None OR drop all columns where the header is None.
from __future__ import annotations
import pandas as pd
d = [[1, 2, None, None, None], [4, 5, None, None, 7]]
cols = ['a', 'b', 'c', None, None]
df = pd.DataFrame(data=d, columns=cols)
print("Original: \n", df)
#Original:
# a b c NaN NaN
#0 1 2 None None NaN
#1 4 5 None None 7.0
print("\nDropped how = all: \n", df.dropna(axis=1, how="all")) # Drops column 'c'
#Dropped how = all:
# a b NaN
#0 1 2 NaN
#1 4 5 7.0
print("\nDropped None columns: \n", df[df.columns.dropna()])
#Dropped None columns:
# a b c
#0 1 2 None
#1 4 5 None
How can I drop only the columns I want to drop and get this?
#Wanted:
# a b c NaN
#0 1 2 None NaN
#1 4 5 None 7.0