【问题标题】:Pandas: How to get the average of all averages in pandas after groupby熊猫:如何在groupby之后获得熊猫所有平均值的平均值
【发布时间】:2023-02-17 15:33:06
【问题描述】:

我在 Pandas 工作,下面是数据框

# initialize list of lists
data = [['A','Excel','1'], ['A','Word','0'], ['A','Java','1'],['B','Excel','1'],['B','Word','0'],['C','Word','0'],['D','Java','1'],['E','PPT','0'], ['E','Word','0'], ['E','Java','1']]
  
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['System','App','DevTool'])

我使用下面的方法获得了每个系统的 DevTool 平均值,但是如何获得所有平均值的总平均值

df.groupby('System')['DevTool'].mean()*100
System DevTool Ratio
A 66.67
B 50.00
C 00.00
D 100.00
E 33.33

请指教。

【问题讨论】:

  • df['DevTool'].mean()*100
  • 您是在问如何计算 np.array 的平均值吗?

标签: pandas dataframe


【解决方案1】:

IIUC 使用:

df['DevTool'] = df['DevTool'].astype(int)

s = df.groupby('System')['DevTool'].mean()*100
s.loc['total avg'] = df['DevTool'].mean()*100

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 2019-07-27
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多