Skip to main content
  1. About
  2. For Teams
Asked
Viewed 24k times
26

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?

2 Answers 2

57

You can simply write:

df = df[['bob']]

and the other columns will be garbage collected.

Sign up to request clarification or add additional context in comments.

Comments

6

you can use df.loc accessor

df=df.loc[:,['bob']]

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.