我是一个完全的Python新手,我正试图解决以下问题…
我在一个excel文件中读到了熊猫,看起来像这样:
<表类>
国家
2000
2001
2002
2003
2004
…
tbody><<tr>阿根廷 1.23 4.56 7.89 10.11 12.13 … 巴西14.15 16.17 18.19 20.21 22.23 … 智利… … … … … … 哥伦比亚… … … … … … 表类>
熊猫。Melt对于您解除数据框架的枢轴是很有用的。
https://pandas.pydata.org/docs/reference/api/pandas.melt.html
因此,您可以将数据框转换为您想要的格式,如下所示:
>>> import pandas as pd
>>> pd.__version__
'1.2.4'
>>> df = pd.DataFrame({
... "Country": ["Argentina", "Brazil",],
... "2000": [1.23, 14.15,],
... "2001": [4.56, 16.17,],
... "2002": [7.89, 18.19,],
... "2003": [10.11, 20.21,],
... "2004": [12.13, 22.23,],
... }) # Create sample data
>>> df
Country 2000 2001 2002 2003 2004
0 Argentina 1.23 4.56 7.89 10.11 12.13
1 Brazil 14.15 16.17 18.19 20.21 22.23
>>> df.melt(id_vars="Country", var_name="Year", value_name="Value")
Country Year Value
0 Argentina 2000 1.23
1 Brazil 2000 14.15
2 Argentina 2001 4.56
3 Brazil 2001 16.17
4 Argentina 2002 7.89
5 Brazil 2002 18.19
6 Argentina 2003 10.11
7 Brazil 2003 20.21
8 Argentina 2004 12.13
9 Brazil 2004 22.23
>>> # If you want to sort the records by "Country":
>>> df.melt(id_vars="Country", var_name="Year", value_name="Value").sort_values("Country").reset_index(drop=True)
Country Year Value
0 Argentina 2000 1.23
1 Argentina 2001 4.56
2 Argentina 2002 7.89
3 Argentina 2003 10.11
4 Argentina 2004 12.13
5 Brazil 2000 14.15
6 Brazil 2001 16.17
7 Brazil 2002 18.19
8 Brazil 2003 20.21
9 Brazil 2004 22.23
>>> import pandas as pd
>>> pd.__version__
'1.2.4'
>>> df = pd.DataFrame({
... "Country": ["Argentina", "Brazil",],
... "2000": [1.23, 14.15,],
... "2001": [4.56, 16.17,],
... "2002": [7.89, 18.19,],
... "2003": [10.11, 20.21,],
... "2004": [12.13, 22.23,],
... }) # Create sample data
>>> df
Country 2000 2001 2002 2003 2004
0 Argentina 1.23 4.56 7.89 10.11 12.13
1 Brazil 14.15 16.17 18.19 20.21 22.23
>>> df.melt(id_vars="Country", var_name="Year", value_name="Value")
Country Year Value
0 Argentina 2000 1.23
1 Brazil 2000 14.15
2 Argentina 2001 4.56
3 Brazil 2001 16.17
4 Argentina 2002 7.89
5 Brazil 2002 18.19
6 Argentina 2003 10.11
7 Brazil 2003 20.21
8 Argentina 2004 12.13
9 Brazil 2004 22.23
>>> # If you want to sort the records by "Country":
>>> df.melt(id_vars="Country", var_name="Year", value_name="Value").sort_values("Country").reset_index(drop=True)
Country Year Value
0 Argentina 2000 1.23
1 Argentina 2001 4.56
2 Argentina 2002 7.89
3 Argentina 2003 10.11
4 Argentina 2004 12.13
5 Brazil 2000 14.15
6 Brazil 2001 16.17
7 Brazil 2002 18.19
8 Brazil 2003 20.21
9 Brazil 2004 22.23
谢谢:)