旋转数据框的 pandas ,重复的行
我在转熊猫时遇到了一些麻烦.我正在处理的dataframe
(日期,位置,数据)如下:
I'm having a little trouble with pivoting in pandas. The dataframe
(dates, location, data) I'm working on looks like:
dates location data
date1 A X
date2 A Y
date3 A Z
date1 B XX
date2 B YY
基本上,我试图在位置上旋转以得到像这样的数据框:
Basically, I'm trying to pivot on location to end up with a dataframe like:
dates A B C
date1 X XX etc...
date2 Y YY
date3 Z ZZ
不幸的是,当我旋转时,相当于原始日期列的索引没有变化,我得到了:
Unfortunately when I pivot, the index, which is equivalent to the original dates column, does not change and I get:
dates A B C
date1 X NA etc...
date2 Y NA
date3 Z NA
date1 NA XX
date2 NA YY
有人知道我该如何解决此问题以获得我正在寻找的数据框格式?
Does anyone know how I can fix this issue to get the dataframe formate I'm looking for?
我目前这样称呼Pivot:
I'm current calling Pivot as such:
df.pivot(index="dates", columns="location")
因为我有#列要轮换的数据列(不想将每个列都列为参数).我相信默认情况下,pivot会旋转数据框中的其余列. 谢谢.
because I have a # of data columns I want to pivot (don't want to list each one as an argument). I believe by default pivot pivots the rest of the columns in the dataframe. Thanks.
如果您有多个数据列,则调用没有值列的数据透视表应为您提供一个以MultiIndex作为列的数据透视表框架:
If you have multiple data columns, calling pivot without the values columns should give you a pivoted frame with a MultiIndex as the columns:
In [3]: df
Out[3]:
columns data1 data2 index
0 a -0.602398 -0.982524 x
1 a 0.880927 0.818551 y
2 b -0.238849 0.766986 z
3 b -1.304346 0.955031 x
4 c -0.094820 0.746046 y
5 c -0.835785 1.123243 z
In [4]: df.pivot('index', 'columns')
Out[4]:
data1 data2
columns a b c a b c
index
x -0.602398 -1.304346 NaN -0.982524 0.955031 NaN
y 0.880927 NaN -0.094820 0.818551 NaN 0.746046
z NaN -0.238849 -0.835785 NaN 0.766986 1.123243