How to label rows by unique pairs of other rows in pandas 0.19.2

前端 未结 2 1005
谎友^
谎友^ 2021-01-23 19:14

I have a dataframe df like this but much larger.

  ID_0 ID_1  location
0    a    b     1
1    a    c     1
2    a    b     0
3    d    c     0
4             


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-23 19:49

    You need GroupBy.ngroup, new in 0.20.2:

    df['group_ID'] = df.groupby(['ID_0', 'ID_1']).ngroup()
    print (df)
      ID_0 ID_1  location  group_ID
    0    a    b         1         0
    1    a    c         1         1
    2    a    b         0         0
    3    d    c         0         2
    4    a    c         0         1
    5    a    c         1         1
    

    df['group_ID'] = df.groupby(['ID_0', 'ID_1']).grouper.group_info[0]
    print (df)
      ID_0 ID_1  location  group_ID
    0    a    b         1         0
    1    a    c         1         1
    2    a    b         0         0
    3    d    c         0         2
    4    a    c         0         1
    5    a    c         1         1
    

提交回复
热议问题