大 pandas 旋转数据框,重复行

2022-01-22 00:00:00 python pandas pivot pivot-table

问题描述

我在 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.


解决方案

如果您有多个数据列,则在没有值列的情况下调用 pivot 应该会给您一个以 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

相关文章