我有以下的
df
,
id a_id b_id
1 25 50
1 25 50
2 26 51
2 26 51
3 25 52
3 28 52
3 28 52
我有以下代码要分配
a_id
和
b_id
到
-1
,基于每个行的行数
id
价值在
东风
如果每个
阿伊德
或
ByID
值与的特定值具有完全相同的行/子df
身份证件
有,那排
阿伊德
和
ByID
获得- 1;
cluster_ids = df.loc[df['id'] > -1]['id'].unique()
types = ['a_id', 'b_id']
for cluster_id in cluster_ids:
rows = df.loc[df['id'] == cluster_id]
for type in types:
ids = rows[type].values
match_rows = df.loc[df[type] == ids[0]]
if match_rows.equals(rows):
df.loc[match_rows.index, type] = -1
所以结果df看起来像,
id a_id b_id
1 25 -1
1 25 -1
2 -1 -1
2 -1 -1
3 25 -1
3 28 -1
3 28 -1
我想知道是否有更有效的方法来做这件事。