Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select everything but a list of columns from pandas dataframe

Tags:

python

pandas

Is it possible to select the negation of a given list from pandas dataframe?. For instance, say I have the following dataframe

T1_V2  T1_V3 T1_V4 T1_V5 T1_V6 T1_V7 T1_V8
1     15      3      2     N     B     N         
4     16     14      5     H     B     N            
1     10     10      5     N     K     N  

and I want to get out all columns but column T1_V6. I would normally do that this way:

df = df[["T1_V2","T1_V3","T1_V4","T1_V5","T1_V7","T1_V8"]]

My question is on whether there is a way to this the other way around, something like this

df = df[!["T1_V6"]]
like image 938
Mohamed Ali JAMAOUI Avatar asked Aug 16 '15 07:08

Mohamed Ali JAMAOUI


People also ask

How do you exclude columns in a DataFrame?

You can use the following syntax to exclude columns in a pandas DataFrame: #exclude column1 df. loc[:, df. columns!='

How do you drop all columns except some in pandas?

Select All Except One Column Using drop() Method in pandas In order to remove columns use axis=1 or columns param. For example df. drop("Discount",axis=1) removes Discount column by kepping all other columns untouched. This gives you a DataFrame with all columns with out one unwanted column.


4 Answers

Do:

df[df.columns.difference(["T1_V6"])]

Notes from comments:

  • This will sort the columns. If you don't want to sort call difference with sort=False

  • The difference won't raise error if the dropped column name doesn't exist. If you want to raise error in case the column doesn't exist then use drop as suggested in other answers: df.drop(["T1_V6"]) `

like image 156
Pekka Avatar answered Oct 21 '22 12:10

Pekka


For completeness, you can also easily use drop for this:

df.drop(["T1_V6"], axis=1)
like image 45
joris Avatar answered Oct 21 '22 13:10

joris


Another way to exclude columns that you don't want:

df[df.columns[~df.columns.isin(['T1_V6'])]]
like image 25
Sharhabeel Hamdan Avatar answered Oct 21 '22 13:10

Sharhabeel Hamdan


I would suggest using DataFrame.drop():

columns_to _exclude = ['T1_V6']
old_dataframe = #Has all columns
new_dataframe = old_data_frame.drop(columns_to_exclude, axis = 1)

You could use inplace to make changes to the original dataframe itself

old_dataframe.drop(columns_to_exclude, axis = 1, inplace = True)
#old_dataframe is changed
like image 33
yesemsanthoshkumar Avatar answered Oct 21 '22 12:10

yesemsanthoshkumar