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

前端 未结 5 920
我寻月下人不归
我寻月下人不归 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:21

    Use groupby.rank function. Here the working example.

    df = pd.DataFrame({'C1':['a', 'a', 'a', 'b', 'b'], 'C2': [1, 2, 3, 4, 5]})
    df
    
    C1 C2
    a  1
    a  2
    a  3
    b  4
    b  5
    
    df["RANK"] = df.groupby("C1")["C2"].rank(method="first", ascending=True)
    df
    
    C1 C2 RANK
    a  1  1
    a  2  2
    a  3  3
    b  4  1
    b  5  2
    
    

提交回复
热议问题