繁体   English   中英

将特定行转置为 pandas 中的列

[英]Transpose specific rows into columns in pandas

我有一个数据集,其中包含如下信息:

Title                         Year                 Revenue (Mi)
---------------------------------------------------------------
Guardians of the Galaxy       2015                   313.13
Split                         2016                   138.0
Sing                          2016                   270.32
Moana                         2015                   248.02

我想将这段数据转换为这种形式:

Title                        2015                      2016
------------------------------------------------------------
Guardians of the Galaxy     313.13
Split                                                 138.02
Sing                                                  270.32
Moana                       248.02

如何将我的数据框转换为这种形式? 在这种情况下,我不知道该怎么做。

尝试

out = df.pivot(index='Title', columns='Year', values='Revenue (Mi)').reset_index()

暂无
暂无

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

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