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.
So df_A
as output would have columns 'ABCDEFGH'
Use filter.
df_A.filter(df_B.columns)
Or
df_A[df_B.columns]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.