【发布时间】:2021-09-17 23:55:26
【问题描述】:
我有一个如下所示的 pandas 数据框
df = pd.DataFrame({'sub_id': [101,101,101,102,102,103,104,104,105],
'test_id':['A1','A1','C1','A1','B1','D1','E1','A1','F1'],
'dummy':['hi','hello','how','are','you','am','fine','thank','you']})
我希望sub_id 和test_id 的每个组合都有一个唯一的ID(序列号)
请注意one subject can have duplicate test_ids but dummy values will be different。
同样,multiple subjects can share the same test_ids 如示例数据框所示
所以,我尝试了以下两种方法,但它们都不正确。
df.groupby(['sub_id','test_id']).cumcount()+1 # incorrect
df['seq_id'] = df.index + 1 # incorrect
我希望我的输出如下所示
【问题讨论】:
标签: python pandas dataframe numpy pandas-groupby