I am trying to drop columns from a dataframe but not working.

columns_to_retain = 'name, sex, age'    # a string of columns 
                                        # which will not be dropped 
                                        # from the dataframe
x_cols = columns_to_retain.split(',') 

new_df = df.drop(columns = not x_cols)  

The new_df should retain the data and columns name, sex and age.

2 Answers

0
Suyash Sreekumar On

Suppose you want to drop You can do this in two ways.

  1. Using drop to remove for eg two columns a and b
import pandas as pd 
df=pd.read_csv('filename') 
df=df.drop(columns=["a","b"])

2.By selecting only the columns you want to display

import pandas as pd
 df=pd.read_csv('filename')
 new_df=df[["name","sex","age"]]

In your case

columns_to_retain = 'name, sex, age'

x_cols = [col.strip() for col in columns_to_retain.split(',')]#column.strip ensures that that are no leading or trailing white space in the words

new_df=df[x_cols]
0
nfreundl On

Suyash's answer should work. If you have the list of columns to drop in a string, you would need to take care of stripping the white-spaces after splitting.

df = pd.DataFrame([[4, 5, 5], [4,6,7]], columns=['a', 'b', 'c'])

to_drop = 'b, c'
to_drop_stripped = [x.strip() for x in to_drop.split(',')]
df.drop(columns=to_drop_stripped)

Result:
   a
0  4
1  4