【问题标题】:Pivot table based on groupby in PandasPandas 中基于 groupby 的数据透视表
【发布时间】:2019-01-03 15:16:22
【问题描述】:

我有一个这样的数据框:

customer_id | date     | category
1           | 2017-2-1 | toys
2           | 2017-2-1 | food
1           | 2017-2-1 | drinks
3           | 2017-2-2 | computer
2           | 2017-2-1 | toys
1           | 2017-3-1 | food

>>> import pandas as pd
>>> dt = dict(customer_id=[1,2,1,3,2,1],
              date='2017-2-1 2017-2-1 2017-2-1 2017-2-2 2017-2-1 2017-3-1'.split(),
              category=["toys", "food", "drinks", "computer", "toys", "food"])) 
>>> df = pd.DataFrame(dt)

使用我的新列和一个热编码这些列,我知道我可以使用df.pivot_table(index = ['customer_id'], columns = ['category'])

>>> df['Indicator'] = 1 
>>> df.pivot_table(index=['customer_id'], columns=['category'],
                   values='Indicator').fillna(0).astype(int)                                                             
category     computer  drinks  food  toys
customer_id                              
1                   0       1     1     1
2                   0       0     1     1
3                   1       0     0     0
>>>  

我还想按date 分组,因此每一行只包含来自同一日期的信息,如下面所需的输出所示,id 1 有两行,因为date 列中有两个唯一日期。

customer_id | toys | food | drinks | computer 
1           | 1    | 0    | 1      | 0        
1           | 0    | 1    | 0      | 0
2           | 1    | 1    | 0      | 0
3           | 0    | 0    | 0      | 1

【问题讨论】:

    标签: python-3.x pandas dataframe group-by pivot-table


    【解决方案1】:

    你可以找crosstab

    >>> pd.crosstab([df.customer_id,df.date], df.category)                                                                                                                
    category              computer  drinks  food  toys
    customer_id date                                  
    1           2017-2-1         0       1     0     1
                2017-3-1         0       0     1     0
    2           2017-2-1         0       0     1     1
    3           2017-2-2         1       0     0     0
    >>>
    >>> pd.crosstab([df.customer_id,df.date],
                    df.category).reset_index(level=1)                                                                                           
    category         date  computer  drinks  food  toys
    customer_id                                        
    1            2017-2-1         0       1     0     1
    1            2017-3-1         0       0     1     0
    2            2017-2-1         0       0     1     1
    3            2017-2-2         1       0     0     0
    >>>
    >>> pd.crosstab([df.customer_id, df.date], 
                    df.category).reset_index(level=1, drop=True)                                                                                
    category     computer  drinks  food  toys
    customer_id                              
    1                   0       1     0     1
    1                   0       0     1     0
    2                   0       0     1     1
    3                   1       0     0     0
    >>>   
    

    【讨论】:

    • 在这种情况下是否可以按每个日期获取单独的行向量? IE。日期应在左上角,其他列和相应行保持不变。也许通过循环每个日期
    【解决方案2】:

    假设你的框架名为df,你可以添加一个指标列,然后直接使用.pivot_table

    df['Indicator'] = 1
    
    pvt = df.pivot_table(index=['date', 'customer_id'],
                         columns='category',
                         values='Indicator')\
            .fillna(0)
    

    这给出了一个如下所示的数据框:

    category              computer  drinks  food  toys
    date     customer_id                              
    2017-2-1 1                 0.0     1.0   0.0   1.0
             2                 0.0     0.0   1.0   1.0
    2017-2-2 3                 1.0     0.0   0.0   0.0
    2017-3-1 1                 0.0     0.0   1.0   0.0
    

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 2018-09-07
      • 1970-01-01
      • 2018-06-27
      • 2016-09-11
      • 2022-10-17
      • 1970-01-01
      • 2019-12-30
      • 2014-07-04
      相关资源
      最近更新 更多