简体   繁体   English

在Python / Pandas中重新排列数据:将特定的列vlaues转换为标题

[英]Rearranging Data in Python/Pandas: Turn specific column vlaues into headers

I have been given a large dataset that has data arranged like so: 我得到了一个大型数据集,其数据排列如下:

location  cost    year
1         23.15    1986
1         23.91    1988
1         23.31    1989
1         23.91    1993
1         22.98    1994
1         23.99    1995
1         23.71    1997
1         23.01    1999
2         23.21    2000
2         24.28    2004
2         24.4     2005

I'd like to rearrange this so it is in the form: 我想重新安排它,使其形式为:

location    1985    1986    1987   1988
1           20.00   20.00   20.0    20.0
2           20.00   20.00   20.0    20.0
3           20.00   20.00   20.0    20.0
4           20.00   20.00   20.0    20.0
5           20.00   20.00   20.0    20.0

(note: ignore that the new costs are all 20.0. My goal is to turn the values within the year column into headers, so that each location is only listed once, with the cost for a specific year located in that column.) (请注意:忽略所有新成本都是20.0。我的目标是将year列中的值转换为标题,以便每个location仅列出一次,而特定年份的cost位于该列中。)

Is there a straightforward way to do this? 有没有简单的方法可以做到这一点? I've looked into groupy and transpose but have not been able to produce anything close to what I'd like. 我研究了成groupytranspose但无法产生任何接近我想要的东西。

Thank you in advance for any pointers you can provide. 在此先感谢您提供的任何提示。

You need to use pivot_table : 您需要使用pivot_table

pd.pivot_table(df, index='location', columns='year', values='cost', fill_value=0)

With your sample: 与您的样品:

#Out[11]: 
#year       1986   1988   1989   1993   1994   1995   1997   1999   2000  \
#location                                                                  
#1         23.15  23.91  23.31  23.91  22.98  23.99  23.71  23.01   0.00   
#2          0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00  23.21   

#year       2004  2005  
#location               
#1          0.00   0.0  
#2         24.28  24.4

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

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