简体   繁体   中英

How to filter one dataframe's columns with another dataframe's MultiIndex

How should I take a set of columns of one dataframe that take values in another set of columns of a different dataframe?

Basically, what I am trying to achieve is like the following:

df.loc[  df[['c1','c2']].isin(df2.index),  :  ] = [1,2,3,4,5]

But this code does not work. How should I achieve this?

One way is to make sure you are comparing one index with another:

df.loc[df.set_index(['c1','c2']).index.isin(df2.index), :] = [1,2,3,4,5]

This will only work if there are guaranteed to be 5 rows filtered.

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.

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