2 回答
TA贡献1827条经验 获得超8个赞
这是一个例子。但是,我需要添加一个辅助列:
df['aux_col'] = df.groupby('id')['worked'].transform('nunique')
final_df = df[(df['aux_col'] == 1) & (df['worked'] == 'yes')]
final_df = final_df.drop(columns='aux_col')
print(final_df)
输出:
id worked
2 B yes
3 B yes
6 D yes
7 D yes
如您所述进行双重检查:
print(final_df['id'].unique())
回报:
['B' 'D']
TA贡献1963条经验 获得超6个赞
您可以尝试df.duplicated:
>>> df2 = df1[df1.duplicated(keep=False)]
>>> df2[df2.worked == 'yes']
id worked
3 B yes
4 B yes
7 D yes
8 D yes
或使用逻辑与 ( &):
>>> f1[df1.duplicated(keep=False) & (df1.worked == 'yes')]
id worked
3 B yes
4 B yes
7 D yes
8 D yes
添加回答
举报
