0

I have a dataframe that contains a large number of columns. I classified these columns and got three lists, each of which contains some column names. For instance:

l1 = ['Year','Day','Month']

l2 = ['A1','A2','A3',...,'A50']

l3 = ['B1','B2','B3',...,'B50']

I want to get a new dataframe that only contains columns within the above three lists.

I tried

df_new = df[l1] + df[l2] + df[l3]

But the result is not correct because the data types are different. I don't think merge, join, or concatenate is necessary because the length of columns is the same and they don't have any common column name. Could you help me with a handy way to do this?

1 Answer 1

2

You would need to add the lists first to get all the column names in a single list and then use it to get part of the dataframe. Try:

df_new = df[[l1 + l2 + l3]]
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.