如何根据大Pandas的发生情况重新标注id

2024-04-25 04:55:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的数据

id   Product
3    ye
4    rt
3    re
4    ri
52   rs
34   rd
32   re
34   rd
32   re
3    re

我想创建新的\u id和事件,以便更容易看到此表中的事件

id   Product  new_id  occurence
3    ye       1       1
4    rt       2       1
3    re       1       2
4    ri       2       2
52   rs       3       1
34   rd       4       1
32   re       5       1
34   re       4       2
32   re       5       2
3    re       1       3

Tags: 数据reidnew事件rdproductrs
2条回答

下面是一种基于dict和groupby cumcount()的方法,即

new = dict(zip(df.drop_duplicates(['id'])['id'],df.reset_index().index+1))
df['new_id'] = df['id'].map(new)

df['occurance'] = df.groupby('id').cumcount()+1
   id Product  occurance  new_id
0   3      ye          1       1
1   4      rt          1       2
2   3      re          2       1
3   4      ri          2       2
4  52      rs          1       3
5  34      rd          1       4
6  32      re          1       5
7  34      rd          2       4
8  32      re          2       5
9   3      re          3       1

选项1

g = df.groupby('id')
df.assign(new_id=g.ngroup() + 1, occurence=g.cumcount() + 1)

   id Product  new_id  occurence
0   3      ye       1          1
1   4      rt       2          1
2   3      re       1          2
3   4      ri       2          2
4  52      rs       5          1
5  34      rd       4          1
6  32      re       3          1
7  34      rd       4          2
8  32      re       3          2
9   3      re       1          3

选项2

df.assign(
    new_id=df.id.factorize()[0] + 1,
    occurence=df.groupby('id').cumcount() + 1)

   id Product  new_id  occurence
0   3      ye       1          1
1   4      rt       2          1
2   3      re       1          2
3   4      ri       2          2
4  52      rs       3          1
5  34      rd       4          1
6  32      re       5          1
7  34      rd       4          2
8  32      re       5          2
9   3      re       1          3

相关问题 更多 >