大 pandas 旋转数据框,重复行 [英] pandas pivoting a dataframe, duplicate rows

查看:31
本文介绍了大 pandas 旋转数据框,重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于大 pandas 旋转数据框,重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆