Pandas 在 Python 中将某些行转换为列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48958035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:14:03  来源:igfitidea点击:

Pandas Convert Some Rows to Columns in Python

pythonpython-2.7pandas

提问by DYZ

So my dataset has some information by business n dates as below:

所以我的数据集有一些业务 n 日期的信息,如下所示:

Business    Date    Value
a         1/1/2017   127
a         2/1/2017   89
b         2/1/2017   122
a         1/1/2018   555
a         2/1/2018   455

I need this data as below format: How can i tranpose it . And i dont want multilevel in my output dataset

我需要以下格式的数据:我如何转置它。我不想在我的输出数据集中多级

Business    1/1/2017  2/1/2017 1/1/2018  2/1/2018
 a           127         89     555        455
 b           N/A        122      N/A       N/A

I tried below syntax:

我尝试了以下语法:

df = df.set_index(['Business','Date'])['Value'].unstack()
df=df.pivot(index='Business', columns='Date', values='Value')

i got the output as below:

我得到如下输出:

Date    1/1/2017    2/1/2017    1/1/2018    2/1/2018
Business
 a        454         5555        555         444 
 b        -            444         -           -

when i print columns, it doesn't show LOB as column. My final dataframe should also include Business,Date fields as columns so that i can join this dataframe with another dataframe on business

当我打印列时,它不会将 LOB 显示为列。我的最终数据框还应包括业务、日期字段作为列,以便我可以将此数据框与业务上的另一个数据框连接起来

回答by DYZ

You are very close to what you want. All you need is to remove the custom index and replace it with the default index.

你非常接近你想要的。您所需要的只是删除自定义索引并将其替换为默认索引。

pivoted = df.pivot(index='Business', columns='Date', values='Value')\
            .reset_index()
pivoted.columns.name=None
print(pivoted)
#  Business  1/1/2017  1/1/2018  2/1/201  2/1/2017
#0        a     127.0     555.0    455.0      99.0
#1        b       NaN       NaN      NaN     122.0

回答by zipa

Use pivot:

使用枢轴

df.pivot(index='Business', columns='Date', values='Value')