-1

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.

dJudge
  • 186
  • 2
  • 17
  • Possible duplicate of [How to delete all columns in DataFrame except certain ones?](https://stackoverflow.com/questions/45846189/how-to-delete-all-columns-in-dataframe-except-certain-ones) – Endre Both Apr 28 '19 at 07:22

2 Answers2

0

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]
  • My problem is that the columns are in a string. ```columns_to_retain = 'name, sex, age'```. How can I interpolate it in the dataframe? – dJudge Apr 28 '19 at 07:38
  • @dJudge Since your column names are all in one string ,what you require to do is convert them to list by splitting on ',' and then pass the list as a parameter. I have edited my answer according to your requirement. Hope it helps – Suyash Sreekumar Apr 30 '19 at 14:08
0

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

nfreundl
  • 11
  • 1