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

前端 未结 2 981
谎友^
谎友^ 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:58

    This should do the trick without using the GroupBy.ngroup which is only supported in newer pandas versions:

    df['group_ID'] = df.groupby(['ID_0', 'ID_1']).grouper.group_info[0]
    
        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
    

    Find more information at this SO post: Python Pandas: How can I group by and assign an id to all the items in a group?

提交回复
热议问题