简体   繁体   English

根据 dataframe B 中的列过滤 dataframe A

[英]Filter dataframe A based on columns from dataframe B

I have two dataframes where dataframe A has much more columns than dataframe B, what i would like to do is filter dataframe A by using dataframe B as reference and obtain a new dataframe A with the same amount of columns that dataframe A has. I have two dataframes where dataframe A has much more columns than dataframe B, what i would like to do is filter dataframe A by using dataframe B as reference and obtain a new dataframe A with the same amount of columns that dataframe A has. For example:例如:

df_A = pd.DataFrame(np.random.randn(150, 17), columns=list('ABCDEFGHIJKLMONPQ'))
df_B = pd.DataFrame(np.random.randn(150, 8), columns=list('ABCDEFGH'))

I would like to filter out the extra columns in df_A and have a df_A with the same columns that df_B has.我想过滤掉df_B中的额外列,并让df_Adf_A具有相同的列。

So df_A as output would have columns 'ABCDEFGH'所以df_A as output 将有列 'ABCDEFGH'

Use filter.使用过滤器。

df_A.filter(df_B.columns)

Or或者

df_A[df_B.columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM