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 flatten a dataframe and combine column and row labels?

I have a dataframe like so

     0210  0222  0525
001     1     4     7
002     2     5     8
003     3     6     9

How do I flatten it and combine the labels as such?

0210-001    1
0210-002    2
0210-003    3
0222-001    4
0222-002    5
0222-003    6
0525-001    7
0525-002    8
0525-003    9

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 :

Use DataFrame.unstack with flatten MultiIndex values:

s = df.unstack()
s.index = [f'{a}-{b}' for a, b in s.index]

print (s)
0210-001    1
0210-002    2
0210-003    3
0222-001    4
0222-002    5
0222-003    6
0525-001    7
0525-002    8
0525-003    9
dtype: int

Last for DataFrame use:

df = s.rename_axis('a').reset_index(name='b')
print (df)
          a  b
0  0210-001  1
1  0210-002  2
2  0210-003  3
3  0222-001  4
4  0222-002  5
5  0222-003  6
6  0525-001  7
7  0525-002  8
8  0525-003  9
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