pd DataFram中列的列名

2024-10-04 03:27:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据帧,我想重塑它。在

  Country       1991  1992  1993  
Deutschland      1     2     3
Griechenland     4     7     0

如何生成如下所示的新数据帧:

^{pr2}$

Tags: 数据country重塑pr2deutschlandgriechenland
1条回答
网友
1楼 · 发布于 2024-10-04 03:27:34

使用melt(然后按国家和年份对值进行排序并重置索引):

(df.melt('Country', var_name='Year', value_name='Value')
   .sort_values(['Country', 'Year'])
   .reset_index(drop=True))

输出:

^{pr2}$

相关问题 更多 >