简体   繁体   中英

Drop columns in with only NaN values, pandas dataframe

I have a dataframe where i want to drop every row where df['Rank'] < 16 , and when i do df = df['Rank'].where(df['Rank'] < 16) it gives alot of NaN values (as it should), and then to remove these values i could do df = df.dropna ,but the problem is that df.dropna will remove rows that have at least one Nan value, and i want to remove rows where all values are Nan. Please show multiple ways to do it and explain them because i want to learn and not just copy paste code:)

You've got an option 'how', so using the DataFrame.dropna function:

df = df.dropna(axis="columns", how="all")

or

df.dropna(axis="columns", how="all", inplace=True)

you will remove the columns with only NA values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM