简体   繁体   中英

I want to find some columns in my data maximum value is not 1

I have 254 rows x 474 columns DataFrame data.

I want to find the columns which column's maximum value is not 1. Most of column's maximum value is 1, but some of column's maximum value is not 1.

I use python 3.x environment.

import pandas as pd

data = pd.read_csv("data.csv") # this is my data.

Use DataFrame.loc with : for get all rows and DataFrame.max for filter columns:

df = pd.DataFrame({'a':[0,2,1],
                   'b':[0,0,1],
                   'c':[3,2,1]})

df1 = df.loc[:, df.max() != 1]
print (df1)
   a  c
0  0  3
1  2  2
2  1  1

If need columns names only:

c = df.columns[df.max() != 1]
print (c)
Index(['a', 'c'], dtype='object')

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