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 transpose m x n into k x 2 form dataframe in pandas

I have m x n form dataframe such as belows.

date1         amt1       date2        amt2
2021-01-02     120      1991-01-02     90
2021-01-03     100      1991-01-03     95
2021-01-04     110      1991-01-04     95
....

Is there any way to transpose into k x 2 form dataframe like…

date          amt
2021-01-02     120      
2021-01-03     100      
2021-01-04     110      
...
1991-01-02     90
1991-01-03     95
1991-01-04     95
...

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 :

This can be done easily with reshape, although a bit different order:

pd.DataFrame(df.to_numpy().reshape(-1, 2), columns=['date', 'amt'])

Output:

         date  amt
0  2021-01-02  120
1  1991-01-02   90
2  2021-01-03  100
3  1991-01-03   95
4  2021-01-04  110
5  1991-01-04   95
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