简体   繁体   中英

Pandas Dataframe- Drop columns if all the values with the column are either 0,1,nan

Assuming this is the dataframe. I looking to drop the columns if all the values within columns are either 0,1, NaN.

df = pd.DataFrame([[1,0,0,0], [0,0,1,0],[2,'NaN',1,0]])

在此处输入图片说明

End result should be just first column "0" and drop remaining columns.

Try:

lst = [0, 1, 'NaN']

mask = df.isin(lst).all(axis=0)
df.drop(mask.loc[mask].index, axis=1, inplace=True)

it will essentially ensure you drop all columns, for which all values are in lst .

use this:

df.drop([1,2])

See also the documentation

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