I have a large dataframe that looks like below.
PERIOD Value
1 30
1 40
1 50
2 60
2 75
2 80
3 90
3 100
3 200
I want to have the DataFrame in following format without using for loop.
PERIOD Value
1 30
2 60
3 90
1 40
2 75
3 100
1 50
2 80
3 200
The order of entries has to be maintained. Any help is appreciated.
>Solution :
You can use groupby.cumcount to (stably) sort the values then use the index to reorder:
order = df.groupby('PERIOD').cumcount().sort_values(kind='stable').index
out = df.loc[order]
Variant using the key parameter of sort_values:
out = df.sort_values(by='PERIOD', kind='stable',
key=lambda x: x.groupby(x).cumcount())
Output:
PERIOD Value
0 1 30
3 2 60
6 3 90
1 1 40
4 2 75
7 3 100
2 1 50
5 2 80
8 3 200