Home > front end >  with data of 100 row and 53 col, i want to extract the columns name which contains only true and fal
with data of 100 row and 53 col, i want to extract the columns name which contains only true and fal

Time:02-25

reducntant app kite jacker reductant2 op1 true FALSE item FALSE op2 FALSE true item4 true op3 true FALSE item2 true op4 true true item4 true

this table is small respresentation of the large 100x53 data i have . How to extract only the names of the columns which has true and false data values? I have around 53 columns and they are a combinations columns different column names,& different columns values but i m interested in only extracting columns that has true and false string data. i used the below code thinking that , i can traverse the csv and extract columns name which has true and false only but it doesnt work. Any help is appreciated!

 for col_name in df.columns:
       if (df[col_name] =='True' and 'False'):
            print(col_name.tolist())
       else :
            print('none')

CodePudding user response:

Pandas infers automatically boolean columns, so the code below should work:

df = pd.read_csv('data.csv')
df = df.select_dtypes(bool)

In the case of it doesn't work, you can use:

df = pd.read_csv('data.csv', dtype=str)  # disable dtype detection
df = df.loc[:, df.isin(['false', 'true']).all()]  # take care of case

CodePudding user response:

try this:

temp = (df.iloc[:,:]).isin(['true','false']).any()#returns a bool pd.Series True=> column contains only 'true' and 'false'
columns_to_drop = [] #column index to be dropped
for i in range(len(temp)):
    if t[i] == False:
        columns_to_drop.append(i)
        
df.drop(df.columns[columns_to_drop], axis = 1, inplace = True) #required data frame
  • Related