Allie Bee
Allie Bee

Reputation: 31

Subsetting a dataframe in pandas according to column name values

I have a dataframe in pandas that i need to split up. It is much larger than this, but here is an example:

ID A  B

a   0  0

b   1  1 

c   2  2

and I have a list: keep_list = ['ID','A'] and another list: recode_list = ['ID','B']

I'd like the split the dataframe up by the column headers into two dataframes: one dataframe with those columns and values whose column headers match the keep_list, and one with those column headers and data that match the recode_alleles list. Every code I have tried thus far has not worked as it is trying to compare the values to the list, not the column names.

Thank you so much in advance for your help!

Upvotes: 2

Views: 463

Answers (2)

Haezer
Haezer

Reputation: 476

Assuming your DataFrame's name is df:

you can simply do df[keep_list] and df[recode_list] to get what you want.

Upvotes: 1

Mayank Porwal
Mayank Porwal

Reputation: 34046

You can do this by index.intersection:

df1 = df[df.columns.intersection(keep_list)]
df2 = df[df.columns.intersection(recode_list)]

Upvotes: 1

Related Questions