python - Pandas : Aggregated and Group by - IDE: Pycharm

标签 python python-3.x pycharm pandas-groupby pandasql

QQ - IDE:Pycharm - 我正在使用以下 Dataframe 示例格式

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

我想按照以下格式聚合它

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

到目前为止我做了什么?

从 pyodbc 导入数据,传递到 pandas 数据框

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

我无法获得所需的输出。使用 pyodbc 时有什么需要特别注意的吗?

最佳答案

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 

关于python - Pandas : Aggregated and Group by - IDE: Pycharm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028960/

相关文章:

Python无法识别请求 header 中的cookie

python - CouldntDecodeError : Decoding failed. ffmpeg 返回错误代码:69

python - import pyttsx 在 python 2.7 中有效,但在 python3 中无效

python - 使用 PyCharm 的 Google App Engine Flask 项目 - sys.path.insert、virtualenv、site-packages

python - Pycharm **kwargs 自动完成

Django:可重用应用程序测试的设置?

python - 如何在 virtualenv 中升级 python 2.7.3 中的 sqlite3?

python - 有没有理由使用 "is"?

python - 使用 Python/Django 集成 MS Power BI

python - 迭代Python字典中元组键的一部分