Isin跨2列进行分组

问题描述

当我知道要在df1中匹配的数据将分布在2列(标题,ID)中时,如何将isin与或(?)结合使用。

如果删除'或df1 [df1.ID.isin(df2 [column])]'

,以下代码将起作用

import pandas as pd
df1 = pd.DataFrame({'Title': ['A1','A2','A3','C1','C2','C3'],'ID': ['B1','B2','B3','D1','D2','D3'],'Whole': ['full','full','semi','semi']})

df2 = pd.DataFrame({'Group1': ['A1','A3'],'Group2': ['B1','B3']})

df = pd.DataFrame()

for column in df2.columns:
    
    d_group = (df1[df1.Title.isin(df2[column])] or df1[df1.ID.isin(df2[column])])
     
    df3 = d_group.groupby('Whole')['Whole'].count()\
                .rename(column,inplace=True)\
                .reindex(['part','semi'],fill_value='-')
    df = df.append(df3,ignore_index=False,sort=False)
        
print(df)

所需的输出:

            | full    | part     | semi
    --------+---------+----------+----------
    Group1  | 3       | -        | -
    Group2  | 3       | -        | -

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)