I want to add a new index to a pandas dataframe

前端 未结 1 2018
醉梦人生
醉梦人生 2021-01-06 15:25

I\'m trying to add a new index to a pandas dataframe. The dataframe looks like this:

                    date  price  neg_vol  pos_vol
0    2017-10-17 01:00:         


        
相关标签:
1条回答
  • 2021-01-06 15:42

    Use ngroup:

    • for new column index
    df['index'] = df.groupby('date', sort=False).ngroup() + 1
    print (df)
                       date  price  neg_vol  pos_vol  index
    0   2017-10-17 01:00:00  51.88       11        4      1
    1   2017-10-17 01:00:00  51.89       10        2      1
    2   2017-10-17 01:00:00  51.90       16       27      1
    3   2017-10-17 01:00:00  51.91        1       10      1
    4   2017-10-17 01:05:00  51.87       12        0      2
    5   2017-10-17 01:05:00  51.88        0       12      2
    6   2017-10-17 01:10:00  51.87        8        0      3
    7   2017-10-17 01:10:00  51.88        0        5      3
    8   2017-10-17 01:15:00  51.87       12        0      4
    9   2017-10-17 01:15:00  51.88        0        8      4
    10  2017-10-17 01:20:00  51.87        6        0      5
    
    • for new index
    df.index = df.groupby('date', sort=False).ngroup() + 1
    print (df)
                      date  price  neg_vol  pos_vol
    1  2017-10-17 01:00:00  51.88       11        4
    1  2017-10-17 01:00:00  51.89       10        2
    1  2017-10-17 01:00:00  51.90       16       27
    1  2017-10-17 01:00:00  51.91        1       10
    2  2017-10-17 01:05:00  51.87       12        0
    2  2017-10-17 01:05:00  51.88        0       12
    3  2017-10-17 01:10:00  51.87        8        0
    3  2017-10-17 01:10:00  51.88        0        5
    4  2017-10-17 01:15:00  51.87       12        0
    4  2017-10-17 01:15:00  51.88        0        8
    5  2017-10-17 01:20:00  51.87        6        0
    

    Another solution is factorize:

    df['index'] = pd.factorize(df['date'])[0] + 1
    

    df.index = pd.factorize(df['date'])[0] + 1
    
    0 讨论(0)
提交回复
热议问题