Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping nan string-columns in panda dataframe

Tags:

python

pandas

df example

I would like to drop all "nan" columns (like the first one on the image) as it doesn't contain any information. I tried this using

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

which unfortunately had no effect. I am afraid that this might be the case because I had to convert my df into a string using

df = df.applymap(str)

This Thread suggests that dropna won't work in such a case, which makes sense to me. I tried to loop over the columns using:

for i in range(len(list(df))):
    if df.iloc[:,i].str.contains('nan').all():
        df.drop(columns=i,axis=1, inplace=True)

which doesn't seem to work. Any help how to drop those columns (and rows as that also doesn't work) is much appreciated

like image 940
xxgaryxx Avatar asked Apr 22 '26 12:04

xxgaryxx


1 Answers

IIUC, try:

df.replace('nan', np.nan).dropna(how='all', axis=1, inplace=True)

This will replace those string 'nan' with np.nan allowing dropna to function as expected.

like image 197
Scott Boston Avatar answered Apr 25 '26 00:04

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!