python - 如何在 Pandas 中执行 groupby 并计算原始数据集中每行的平均值

标签 python pandas dataframe pandas-groupby

我有一个电子表格,其中包含以下格式的数据:

Brand | Model    | Year | Cost  | Tax
--------------------------------------
Apple | iPhone 7 | 2017 | $1000 | $100

Apple | iphone 7 | 2018 | $800  |  $80

Xiomi | Note 5   | 2017 | $300  |  $30

Xiomi | Note 5   | 2018 | $200  |  $20

我想将上面的数据集转换为以下我想要显示 Mean 的数据集当行按['Brand', 'Model']分组时成本列的和一个 结果 列,它是 Mean 的总和和Tax列值:

Brand | Model    | Year | Cost  | Mean   | Tax    |  Result
------------------------------------------------------------ 
Apple | iPhone 7 | 2017 | $1000 | $900   | $100   |  $1000

Apple | iphone 7 | 2018 | $800  | $900   | $80    |  $980

Xiomi | Note 5   | 2017 | $300  | $250   | $30    |  $280

Xiomi | Note 5   | 2018 | $200  | $250   | $25    |  $275

我一直在尝试使用 groupby函数,但无法获得如上所述的所需结果。

期待您的回复。谢谢。

最佳答案

首先使用replace将值转换为整数,通过 transform 得到mean ,然后 sum 并在必要时最后转换回字符串:

cols = ['Cost','Tax']
df[cols] = df[cols].replace('\$','', regex=True).astype(int)
df['Mean'] = df.groupby(['Brand', 'Model'])['Cost'].transform('mean')

df['Result'] = df[['Mean','Tax']].sum(axis=1)
print (df)
   Brand     Model  Year  Cost  Tax  Mean  Result
0  Apple  iPhone 7  2017  1000  100  1000    1100
1  Apple  iphone 7  2018   800   80   800     880
2  Xiomi    Note 5  2017   300   30   250     280
3  Xiomi    Note 5  2018   200   20   250     270

然后:

cols1 = cols + ['Result', 'Mean']
df[cols1] = '$' + df[cols1].astype(str)
print (df)
   Brand     Model  Year   Cost   Tax   Mean Result
0  Apple  iPhone 7  2017  $1000  $100  $1000  $1100
1  Apple  iphone 7  2018   $800   $80   $800   $880
2  Xiomi    Note 5  2017   $300   $30   $250   $280
3  Xiomi    Note 5  2018   $200   $20   $250   $270

关于python - 如何在 Pandas 中执行 groupby 并计算原始数据集中每行的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54591062/

相关文章:

python - 任何人都知道为什么我在尝试将数据帧加载到 sybase 表时出现此错误? [SQL 炼金术]

python - 将 pandas 数据框转换为转置表格格​​式

r - 复杂的 reshape

python - 使用 openpyxl 插入列

python - "continue"是逃离 try catch block 的 Pythonic 方式吗?

python - Pandas :对多列进行逐行操作

python - 有条件聚合 Pandas DataFrame

python - Pandas:如何将具有重复索引值的数据帧转换为字典

python - 根据两个列表的大小定义多个对象(python)

python - 如何将 Twitter json 对象加载到 python 中