python - Pandas - 将具有多列的数据框 reshape /转换为单列值

标签 python pandas

我有一个 pandas 数据框,其中年份作为列,国家/地区作为行名:

Country       | 1960 | 1961 | 1962 | 1963
-----------------------------------------
United States | 1000 | 2000 | 3000 | 4000
-----------------------------------------
Argentina     | 1000 | 2000 | 3000 | 4000
-----------------------------------------

我想把它改造成:

Country       | Year | Value
-----------------------------
Unites States | 1960 | 1000
Unites States | 1961 | 2000
Unites States | 1962 | 3000
Unites States | 1963 | 4000
Argentina     | 1960 | 1000
Argentina     | 1961 | 2000
Argentina     | 1962 | 3000
Argentina     | 1963 | 4000

我不确定需要应用哪些拆分、排序或分组操作才能实现此目标。

谢谢!

最佳答案

举个完整的例子,

In [1]: df = pd.DataFrame([['United States', 1000, 2000, 3000, 4000],
                           ['Argentina', 1000, 2000, 3000, 4000]],
                          columns=['Country', 1960, 1961, 1962, 1963])

In [2]: df.set_index('Country', inplace=True)
In [3]: df = df.stack().reset_index()
In [4]: df.columns = ['Country', 'Year', 'Value']

产量

         Country  Year  Value
0  United States  1960   1000
1  United States  1961   2000
2  United States  1962   3000
3  United States  1963   4000
4      Argentina  1960   1000
5      Argentina  1961   2000
6      Argentina  1962   3000
7      Argentina  1963   4000

要摆脱索引列并使用 Country 列作为索引,您可以使用

In [3]: df = df.stack().reset_index(1)
In [4]: df.columns = ['Year', 'Value']

产生

               Year  Value
Country                   
United States  1960   1000
United States  1961   2000
United States  1962   3000
United States  1963   4000
Argentina      1960   1000
Argentina      1961   2000
Argentina      1962   3000
Argentina      1963   4000

关于python - Pandas - 将具有多列的数据框 reshape /转换为单列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682175/

相关文章:

python - 计算每个用户每月的平均收入

python - 如何按两列共同对 pandas DataFrame 进行排序,而不是一列接着另一列?

python - 保存 Pandas 数据框但保留 NA 值

python - 匹配两个 Pandas 系列: How to find a string element from one series in another series and then create a new column

python - 无法读取的笔记本 : FileNotFoundError(2, 'No such file or directory' )

python - 使用 boto3 清空 s3 存储桶的最快方法是什么?

python - SymPy 无法计算此矩阵的特征值

python - message.content.startswith Discord.Py

python - 在 Python 中写入和读取临时文件时出现 EmptyDataError

python - 使用 dtype 时,pandas read_csv 不解析逗号小数分隔符?