2

QQ - IDE: Pycharm - I am using the below Dataframe Sample format

Name       Business        SegmentID   Revenue    Margin  OrderQuantity
James      Commercial      1001         1500      100     1
Joe        Consumer        1002         800       10      1
James      Commercial      1003         1900      110     2
James      Commercial      1004         1800      105     3
Samuel     Commercial      1005         1800      105     1

I want to aggregated it in the below format

Name      Revenue  Margin  OrderQuantity
James     5200     315     6
Joe       800      10      1
Samuel    1800     105     1

What i have done so far ?

Data import from pyodbc, passed to a pandas dataframe

df.groupby(['Name']).Revenue.sum().Margin.sum().OrderQuantity.sum()

I was Unable to get the desired output. is there something i need to be focusing on specifically while using pyodbc.

4

2 回答 2

2

groupby 聚合是您正在寻找的内容:

例如:

import numpy as np
import pandas as pd

d = {'Name': ['foo1','foo2','foo3','foo2','foo3'], 
'Business': ['bar2','bar3','bar1','bar1','bar1'],
    'ID':['1','2','3','4','5'],
    'Revenue':[10000,12500,7500,3000,15000],
    'Margin':[300,500,100,300,200],
    'Quanity':[1,2,2,3,4]}

df = pd.DataFrame(data=d)

df的输出:

 Business ID  Margin  Name  Quanity  Revenue                                                                           
0     bar2  1     300  foo1        1    10000                                                                           
1     bar3  2     500  foo2        2    12500                                                                           
2     bar1  3     100  foo3        2     7500                                                                           
3     bar1  4     300  foo2        3     3000                                                                           
4     bar1  5     200  foo3        4    15000   

然后使用 groupby:

groupby_df_agg = df.groupby(['Name'])[('Revenue', 'Margin', 'Quanity')].agg(['sum'])

print(groupby_df_agg)

输出

     Revenue Margin Quanity                                                                                             
         sum    sum     sum                                                                                             
Name                                                                                                                    
foo1   10000    300       1                                                                                             
foo2   15500    800       5                                                                                             
foo3   22500    300       6   

要扩展更多分类变量,您可以使用:

groupby_df_agg = df.groupby(['Name','Business'])[('Revenue', 'Margin','Quanity')].agg(['sum'])

输出

              Revenue Margin                                                                                          
                  sum    sum                                                                                          
Name Business                                                                                                         
foo1 bar2       10000    300                                                                                          
foo2 bar1        3000    300                                                                                          
     bar3       12500    500                                                                                          
foo3 bar1       22500    300 
于 2019-03-06T17:39:12.993 回答
1

您可以使用聚合方法。

df.groupby(['Name']).agg({'Revenue':'sum, 'Margin': 'sum', 'OrderQuantity':'sum'})
于 2019-03-06T17:37:01.123 回答