0

I have created a list with 2 column names of a data frame.

Now I want to use this list to select this 2 columns along with other columns.

I have for example a data frame with 5 columns: A, B, C, D, E

I create a list that references A and B:

lst = ['A','B']

Then when I try to print it adding C I get an error.

print(df[lst,'C'])

TypeError: '(['A', 'B'], 'C')' is an invalid key

Which is the correct way to select the columns in a list along with others?

thanks!

1 Answer 1

1

Need to pass that as list too;

df[lst+['C']]
Sign up to request clarification or add additional context in comments.

Comments

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.