简体   繁体   English

从列值的总和构建 df

[英]build df from sum of column value

I need to group the data by customer_id and get the sum of purchase for each months.我需要按 customer_id 对数据进行分组并获取每个月的购买总和。 My data looks like this:我的数据如下所示:

cust_id        months
1               1
1               1
1               2
1               4
2               1
2               1

So I need to see the sum of purchase for each months and each customer.所以我需要查看每个月和每个客户的购买总额。 The desired output is:所需的 output 是:

cust_id     mo1     mo2     mo3     mo4
1           2       1       0       1
1           2       0       0       0

Use crosstab with DataFrame.reindex for add missing categories:使用带有DataFrame.reindexcrosstab来添加缺失的类别:

r = range(df['months'].min(), df['months'].max() + 1)
df = (pd.crosstab(df['cust_id'],df['months'])
        .reindex(r, axis=1, fill_value=0)
        .add_prefix('mo'))
print (df)
months   mo1  mo2  mo3  mo4
cust_id                    
1          2    1    0    1
2          2    0    0    0

If need all months is possible use ordered categoricals:如果需要所有月份都可以使用有序分类:

df['months'] = pd.Categorical(df['months'], ordered=True, categories=range(1, 13))

df = df.groupby(['cust_id','months']).size().unstack(fill_value=0).add_prefix('mo')
print (df)
months   mo1  mo2  mo3  mo4  mo5  mo6  mo7  mo8  mo9  mo10  mo11  mo12
cust_id                                                               
1          2    1    0    1    0    0    0    0    0     0     0     0
2          2    0    0    0    0    0    0    0    0     0     0     0

Or reindex by range for all months:或者按range reindex所有月份:

r = range(1, 13)
df = (pd.crosstab(df['cust_id'],df['months'])
        .reindex(r, axis=1, fill_value=0)
        .add_prefix('mo'))
print (df)
months   mo1  mo2  mo3  mo4  mo5  mo6  mo7  mo8  mo9  mo10  mo11  mo12
cust_id                                                               
1          2    1    0    1    0    0    0    0    0     0     0     0
2          2    0    0    0    0    0    0    0    0     0     0     0

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

相关问题 如何在 df 列值上方添加 sum() 值? - How to add a sum() value above the df column values? 由另一列 pandas df 分组的值出现的总和 - sum of value occurrence grouped by another column pandas df 根据列条件从以前的 df 中提取值到新的 df - extract value from previous df to new df based on column criteria 根据条件从第一个 df 到另一个 df 的列值 - Column value from first df to another df based on condition 根据列值从 df 访问一行 - Access a row from a df based on a column value 从 df 列的列表中过滤期望值 - Filter expected value from list in df column 如果 df1 column1 中的值与列表中的值匹配,Pandas 从另一个 df1 column2 在 df2 中创建新列 - Pandas create new column in df2 from another df1 column2 if a value in df1 column1 matches value in a list 通过行总和和列总和来替换df - Subsetting a df by rows sum and column sum 如何向 dataframe (df1) 添加一个新列,这是另一个 dataframe (df2) 中 df1 的多个查找值的总和 - How can I add a new column to a dataframe (df1) that is the sum of multiple lookup values from df1 in another dataframe (df2) 如果 df2 中的单个列包含来自 df1 中的列的 value2,则在 df1 中创建具有 value1 的新 col4 - Create new col4 in df1 with a value1, if the single column in df2 contains the value2 from a column in df1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM