Home > front end >  How to make an order column when grouping by another column
How to make an order column when grouping by another column

Time:12-04

I have a dataframe in a format:

d = {'hour': [1, 1,2,2], 'value': [10, 50,200,100]}
df = pd.DataFrame(data=d)

How can I create a column order, where order will be an order of values when grouped by the hour column.

The result should be:

index hour  value   order
0      1     10      1
1      1     50      2
2      2     200     2
3      2     100     1

CodePudding user response:

Use GroupBy.rank:

df['order'] = df.groupby('hour')['value'].rank(method='dense').astype(int)
print (df)
   hour  value  order
0     1     10      1
1     1     50      2
2     2    200      2
3     2    100      1

CodePudding user response:

df.sort_values('value', inplace=True)
df['order'] = df.groupby('hour').cumcount()   1
df.sort_index(inplace=True)  # if you wish to keep the original order
  • Related