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

How to re-arrange this df in groups of 3

I have a shortened version of my real data in which I would like to re-arrange as described below.

Input:

import pandas as pd
import numpy as np  
data4 = [[45], [285], [256], [55], [360], [213], [65], [435], [368]]
df4 = pd.DataFrame(data4)

Output:

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

     0
0   45
1  285
2  256
3   55
4  360
5  213
6   65
7  435
8  368
    

Desired Output:

    0    1    2
0  45  285  256
1  55  360  213
2  65  435  368

>Solution :

With np.reshape: pd.DataFrame(df4.values.reshape(-1, 3))

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