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?
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.
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.
To select all columns except one column in Pandas DataFrame, we can use df. loc[:, df. columns != <column name>].
You can simply write:
df = df[['bob']]
and the other columns will be garbage collected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With