简体   繁体   English

pandas - python 中的转置矩阵

[英]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.我正在处理表数据的转换,但现在我有点卡在当前为 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.因此希腊和葡萄牙都有 2005 年和 2006 年的值。 What I need to do is to each row correspond to a Country and a singular year this way:我需要做的是让每一行以这种方式对应一个国家和一个单一的年份:

          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.我正在使用 python 中的数据框。

Why aren't you using the index functionality in pandas?你为什么不使用 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM