简体   繁体   English

一次具有多个列的Pandas数据透视表

[英]Pandas pivot table with multiple columns at once

I wonder if pandas.pivot_table can accept two columns at once and process them separately instead of hierarchically. 我想知道pandas.pivot_table可以一次接受两列并分别处理而不是按层次处理。

Say I have the following data frame: 说我有以下数据框:

 id    date   day  val
101   11/1/1   1   2.1
101   11/1/2   2   2.2
101   11/1/3   3   2.3
102   11/1/2   1   3.1
102   11/1/3   2   3.2
102   11/1/4   3   3.3

I want the result to be like this: 我希望结果是这样的:

      date                            day
 id  11/1/1  11/1/2  11/1/3  11/1/4    1   2   3
101   2.1     2.2      2.3     NaN    2.1 2.2 2.3
102   NaN     3.1      3.2     3.3    3.1 3.2 3.3

When I do df.pivot_table(index='id', columns=['date','day'], values='val') , it will integrate date and day into a hierarchy which is not what I want. 当我执行df.pivot_table(index='id', columns=['date','day'], values='val') ,它将把dateday集成到我不想要的层次结构中。 Of course I can do twice with date and day respectively and concatenate the results, but is there a more convenient way to do so at once? 当然,我可以做两次, dateday分别连接结果,但有一个更便捷的方式这样做一次?

You can make 2 pivot calls and concat enate the result. 你可以让2个pivot电话和concat enate结果。

i = df.pivot('id', 'date', 'val')
j = df.pivot('id', 'day', 'val')

pd.concat([i, j], 1, keys=['date', 'day'])

      date                       day          
    11/1/1 11/1/2 11/1/3 11/1/4    1    2    3
id                                            
101    2.1    2.2    2.3    NaN  2.1  2.2  2.3
102    NaN    3.1    3.2    3.3  3.1  3.2  3.3

As a single liner - 作为单个衬板-

c = ['date', 'day']  # add more cols as needed
pd.concat([df.pivot('id', x, 'val') for x in c], axis=1, keys=c)

      date                       day          
    11/1/1 11/1/2 11/1/3 11/1/4    1    2    3
id                                            
101    2.1    2.2    2.3    NaN  2.1  2.2  2.3
102    NaN    3.1    3.2    3.3  3.1  3.2  3.3

One line ....but why you need them in one line.. 一行....但是为什么需要一行

df.set_index(['id','val']).stack().to_frame('col').\
         reset_index(level='val').set_index('col',append=True).\
              unstack([-2,-1]).sort_index(1,level=1)
Out[69]: 
       val                                    
      date                       day          
col 11/1/1 11/1/2 11/1/3 11/1/4    1    2    3
id                                            
101    2.1    2.2    2.3    NaN  2.1  2.2  2.3
102    NaN    3.1    3.2    3.3  3.1  3.2  3.3

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

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