简体   繁体   English

基于条件和总和列的 Pandas 数据透视表计数

[英]Pandas Pivot Table counting based on condition and sum columns

I have DateFrame as shown below.我有如下所示的 DateFrame。 I need to add a few columns to dft (pivot output).我需要向 dft(枢轴输出)添加几列。 First one to calculate sum of products sold daily, it's like margine but only for sum column not len.第一个计算每天销售的产品总和,这就像marine,但仅适用于总和列而不是len。 I tried dft['Fruit Total']=df.iloc[:,0:3].sum(axis=1) but it didn't work.我试过 dft['Fruit Total']=df.iloc[:,0:3].sum(axis=1) 但没有用。 also I want to add column counting values > than 2 for each row in column sum.我还想为列总和中的每一行添加大于 2 的列计数值。 like in the picture就像在图片中

df = pd.DataFrame({
    'date': ["22.10.2021", "22.10.2021", "22.10.2021", "23.10.2021", "23.10.2021", "25.10.2021", "22.10.2021", "23.10.2021", "22.10.2021", "25.10.2021"],
    'Product': ["apple", "apple", "orange", "orange", "apple","apple", "apple", "orange", "orange", "orange"],
    'sold_kg': [2, 3, 1, 6, 2,2, 3, 1, 6, 2,]})
df['day']=pd.to_datetime(df['date']).dt.day

dft=df.pivot_table(values='sold_kg', columns ='day', index='Product', aggfunc=[np.sum,len])
dft

在此处输入图像描述

Use:利用:

dft=df.pivot_table(values='sold_kg',columns='day', index='Product', aggfunc=['sum','size'])

First flatten MultiIndex in columns with mapping:首先在具有映射MultiIndex in columns

dft.columns = dft.columns.map(lambda x: f'{x[0]}_{x[1]}')

Then select columns by DataFrame.filter and sum , for count values greater or equal use DataFrame.ge and count True s by sum :然后通过DataFrame.filtersum选择列,对于大于或等于的计数值,使用DataFrame.ge并通过sum计数True s:

dft['Fruit Total'] = dft.filter(like='sum').sum(axis=1)

dft['Count >= 2'] = dft.filter(like='size').ge(2).sum(axis=1)
print (dft)
         sum_22  sum_23  sum_25  size_22  size_23  size_25  Fruit Total  \
Product                                                                   
apple         8       2       2        3        1        1           12   
orange        7       7       2        2        2        1           16   

         Count >= 2  
Product              
apple             1  
orange            2           

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

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