Python Pandas相同值的col A具有不同的col B值[重复]

y1aodyip  于 2023-08-01  发布在  Python
关注(0)|答案(1)|浏览(101)

此问题已在此处有答案

Pandas Groupby Select Groups that Have More Than One Unique Values in a Column(5个答案)
Pandas - Keeping groups having at least two different codes(2个答案)
14天前关闭
APandas解决方案值B不相同对于相同A值时获取结果数据框
输入:

colA  colB
a     1 <--
b     2
a     1 <--
a     0 <-- different

字符串
输出:

colA  colB
a     1 <--
a     1 <--
a     0 <-- different


解决方案:
Pandas Groupby Select Groups that Have More Than One Unique Values in a Column

out = df.groupby(['a']).filter(lambda x: x['b'].nunique() > 1)


@mozway评论也有效

out = df[df.groupby('colA')['colB'].transform('nunique').gt(1)]

jfgube3f

jfgube3f1#

result = df[df.duplicated(subset='colA', keep=False) | ~df.duplicated(subset='colA', keep=False)]

字符串
如果你不想重复:

result.drop_duplicates(inplace=True)

相关问题