Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wrangling Pandas Dataframe

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading