2

I have a dataframe with many many columns. I want to reduce this dataframe to one with only the columns I require.

Instead of using

del df['column_name']

for all the columns that I don't need, is there a way to select the ones I do and create a new dataframe? I have tried:

df1['column_name'] = df['column_name']

1 Answer 1

4

You can select the columns in a list:

cols = ['col1', 'col2']
df[cols]
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand why this works and this does not: 'df = df[['col1', 'col2']]'

Your Answer

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.