如何基于另一个数据框中的变量从数据框中选择列

时间:2018-10-25 07:41:19

标签: python pandas dataframe

我只想从df2中选择与python pandas中df1变量相等的列

df1

parameter (column name)

a
b
c

df2

w  x  a  c  z
3  1  5  6  1
5  67 4  3  56
8  12 6  1  23

我的预期输出是

a c
5 6
4 3
6 1

1 个答案:

答案 0 :(得分:0)

intersectionisin用作布尔掩码:

df3 = df2[df.columns.intersection(df1['parameter'])]

或者:

df3 = df2.loc[:, df.columns.isin(df1['parameter'])]