Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all but one column of pandas dataframe?

Tags:

I have a pandas dataframe and would like to drop all columns save the index and a column named 'bob'

How would I do this?

like image 482
goldisfine Avatar asked Sep 28 '13 02:09

goldisfine


People also ask

How delete all columns except one 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.

How do I exclude one column from a DataFrame?

We can exclude one column from the pandas dataframe by using the loc function. This function removes the column based on the location. Parameters: dataframe: is the input dataframe.

How do I select all but one column in pandas?

To select all columns except one column in Pandas DataFrame, we can use df. loc[:, df. columns != <column name>].


1 Answers

You can simply write:

df = df[['bob']] 

and the other columns will be garbage collected.

like image 168
Andy Hayden Avatar answered Oct 25 '22 16:10

Andy Hayden