python-3.x - Pandas中基于groupby的数据透视表

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

我有一个这样的数据框:

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

最佳答案

您可能正在寻找 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
>>>   

关于python-3.x - Pandas中基于groupby的数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51541995/

相关文章:

r - 转换数字矩阵中snp基因型的数据框

python - 如何使用索引替换列值的前两个字母

python - 使用 iloc() 根据范围和单个整数选择列

python - 从个体中减去子组平均值而不求助于 for 循环

python - 将python数据框转换为列表

python - Pandas:使用 iloc 根据条件更改 df 列值

python - 如何在不修改 Python 中的原始列表的情况下反转列表

python - 如何使用python将数据转储到Json文件中

python-3.x - 我如何使用 tf.keras.utils.get_file 加载图像数据集

r - 如何在 R/中执行复杂的多列匹配