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

pandas – transpose matrix in python

I’m working with transformations to table data but now I am kinda stuck in a transpose of a table that is currently a dataframe.

Here is my table:

          0        1       2
0        NA     2005    2006
1    GREECE     78.4    97.6
2  PORTUGAL    127.3   128.1

So both Greece and Portugal has values for the 2005 and 2006 year.
What I need to do is to each row correspond to a Country and a singular year this way:

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       1        2
0        NA    YEAR    VALUE
1    GREECE    2005     78.4    
2    GREECE    2006     97.6
3  PORTUGAL    2005    127.3
4  PORTUGAL    2006    128.1

What would be the best way to achive this? I am working with dataframes in python.

>Solution :

Why aren’t you using the index functionality in pandas? It would make much more sense if the country was the row index, and the year was the columns:

import pandas as pd

df = pd.read_clipboard() # Your df here

df = pd.DataFrame(
    df.iloc[1:, 1:].values,
    columns=df.iloc[0, 1:].values,
    index=df.iloc[1:, 0].values
)
#           2005.0  2006.0
# GREECE      78.4    97.6
# PORTUGAL   127.3   128.1

# Now you can use built-in pandas functionality:
out = df.melt(var_name="YEAR", value_name="VALUE", ignore_index=False)
#             YEAR  VALUE
# GREECE    2005.0   78.4
# PORTUGAL  2005.0  127.3
# GREECE    2006.0   97.6
# PORTUGAL  2006.0  128.1

# Or similarly:
out = df.stack().to_frame("VALUE").reset_index(names=["COUNTRY", "YEAR"])
#     COUNTRY    YEAR  VALUE
# 0    GREECE  2005.0   78.4
# 1    GREECE  2006.0   97.6
# 2  PORTUGAL  2005.0  127.3
# 3  PORTUGAL  2006.0  128.1

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