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?
CodePudding user response:
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]]