SQL-like window functions in PANDAS: Row Numbering in Python Pandas Dataframe

前端 未结 5 907
我寻月下人不归
我寻月下人不归 2020-11-27 03:49

I come from a sql background and I use the following data processing step frequently:

  1. Partition the table of data by one or more fields
  2. For each parti
5条回答
  •  情歌与酒
    2020-11-27 04:24

    you can also use sort_values(), groupby() and finally cumcount() + 1:

    df['RN'] = df.sort_values(['data1','data2'], ascending=[True,False]) \
                 .groupby(['key1']) \
                 .cumcount() + 1
    print(df)
    

    yields:

       data1  data2 key1  RN
    0      1      1    a   1
    1      2     10    a   2
    2      2      2    a   3
    3      3      3    b   1
    4      3     30    a   4
    

    PS tested with pandas 0.18

提交回复
热议问题