比较单个数据帧中相同变量的2个值

问题描述 投票:0回答:2

我有如下数据框:

Obs. ID   Name
  1) 123  abc
  2) 123  abc
  3) 145  abc
  4) 156  abc
  5) 156  abc

如果ID是相同的,例如obs。 1和2或4和5,那么我想创建一个新变量type = duplicate else type = single。

python pandas dataframe string-comparison
2个回答
0
投票

IIUC

df['Dup']=df.ID.duplicated(keep=False)
df
  Obs.   ID Name    Dup
0   1)  123  abc   True
1   2)  123  abc   True
2   3)  145  abc  False
3   4)  156  abc   True
4   5)  156  abc   True

0
投票

我们可以将duplicatedduplicated结合使用以根据结果设置值:

np.where

np.where
© www.soinside.com 2019 - 2024. All rights reserved.