【问题标题】:Creating an Averaged dataframe using pandas使用 pandas 创建平均数据框
【发布时间】:2026-01-17 14:55:01
【问题描述】:
From country    Austria Belgium Denmark France  Germany Italy   Luxembourg  Switzerland The Netherlands United Kingdom
Austria 0   0   0   0   0   0   3   0   6   1
Belgium 0   0   0   2   1   1   0   0   5   1
Denmark 0   2   0   2   0   1   0   2   3   0
France  0   0   0   0   6   0   0   0   4   0
Germany 0   2   0   6   0   0   0   1   1   0
Italy   0   0   3   0   1   0   4   1   1   0
Luxembourg  0   0   0   4   0   1   0   1   3   1
Switzerland 0   1   0   0   0   0   0   0   7   2
The Netherlands 1   0   5   1   0   2   0   0   0   1
United Kingdom  2   0   2   2   0   2   1   0   1   0

这里我有一个表,其中的值是从一个国家分配到一个国家的列上的点。我总共有 60 个表,我正在尝试创建一个看起来相同但值是所有 60 个表的平均值的最终表。我无法在 pandas 或堆栈交换的其他地方找到任何函数来平均每个值,就像我正在尝试做的那样,我该如何解决这个问题?

PS:在某些表格中,国家或多或少

【问题讨论】:

  • 所有表都在文件中?
  • 是的,它们都在一个 xlsx 文件中,我正在使用 pd.read_excel() 将其加载到数据帧中

标签: python excel pandas dataframe average


【解决方案1】:

您可以先将read_excel 与参数sheetname=None 一起用于dictDataframes。然后通过concat创建大df,通过第二级index创建groupby并聚合mean

dict_dfs = pd.read_excel('multiple_sheets.xlsx', sheetname=None)
print (dict_dfs)
{'sheetname1':    a  b
0  1  4
1  2  8, 'sheetname2':    a  b
0  7  1
1  5  0, 'sheetname3':    a  b
0  4  5}

df = pd.concat(dict_dfs)
print (df)
              a  b
sheetname1 0  1  4
           1  2  8
sheetname2 0  7  1
           1  5  0
sheetname3 0  4  5

df = df.groupby(level=1).mean()
print (df)
     a         b
0  4.0  3.333333
1  3.5  4.000000

编辑:

以您的数据为样本file

dict_dfs = pd.read_excel('multiple_sheets.xlsx', sheetname=None, index_col=0)
df = pd.concat(dict_dfs)

df = df.groupby(level=1).mean()
print (df)
                 Austria  Belgium  Denmark  France  Germany  Italy  \
Fromcountry                                                          
Austria                4        0        0       0        0      0   
Belgium                0        0        0       2        1      1   
Denmark                0        2        0       2        0      1   
France                 0        0        0       0        6      0   
Germany                0        2        0       6        0      0   
Italy                  0        0        3       0        1      0   
Luxembourg             0        0        0       4        0      1   
Switzerland            0        1        0       0        0      0   
The Netherlands        1        0        5       1        0      2   
USA                    3        4        0       0        0      0   
United Kingdom         2        0        2       2        0      2   

                 Luxembourg  Switzerland  The Netherlands  USA  United Kingdom  
Fromcountry                                                                     
Austria                   3            0                6  4.0               1  
Belgium                   0            0                5  4.0               1  
Denmark                   0            2                3  5.0               0  
France                    0            0                4  0.0               0  
Germany                   0            1                1  0.0               0  
Italy                     4            1                1  0.0               0  
Luxembourg                0            1                3  0.0               1  
Switzerland               0            0                7  0.0               2  
The Netherlands           0            0                0  0.0               1  
USA                       0            0                0  0.0               0  
United Kingdom            1            0                1  0.0               0

如果有多个 coutries,最后使用 reindex 按引用 indexcolumns 名称进行过滤:

#reference sheetname - sheetname1
idx = dict_dfs['sheetname1'].index
cols = dict_dfs['sheetname1'].columns

df = df.reindex(index=idx, columns=cols)
print (df)
                 Austria  Belgium  Denmark  France  Germany  Italy  \
Fromcountry                                                          
Austria                4        0        0       0        0      0   
Belgium                0        0        0       2        1      1   
Denmark                0        2        0       2        0      1   
France                 0        0        0       0        6      0   
Germany                0        2        0       6        0      0   
Italy                  0        0        3       0        1      0   
Luxembourg             0        0        0       4        0      1   
Switzerland            0        1        0       0        0      0   
The Netherlands        1        0        5       1        0      2   
United Kingdom         2        0        2       2        0      2   

                 Luxembourg  Switzerland  The Netherlands  United Kingdom  
Fromcountry                                                                
Austria                   3            0                6               1  
Belgium                   0            0                5               1  
Denmark                   0            2                3               0  
France                    0            0                4               0  
Germany                   0            1                1               0  
Italy                     4            1                1               0  
Luxembourg                0            1                3               1  
Switzerland               0            0                7               2  
The Netherlands           0            0                0               1  
United Kingdom            1            0                1               0

【讨论】:

  • keys 争论到底是什么?文档并没有真正为我弄清楚“键:序列,默认无如果传递了多个级别,则应包含元组。使用传递的键作为最外层构建分层索引”,并且在同一个注释中,名称列出了什么应该代表?
  • 这很酷,我必须更多地研究 groupby 方法。但是有没有办法保留表格的索引,即:来自国家/地区?
  • 我不确定是否理解 - 国家索引不在 excel 文件中?
  • @GrahamChapman 即使这篇文章不是您选择的答案,它也非常有用并且提供了很多可供学习的内容。赞成这个答案也是合适的。
  • 很高兴能帮到你!
【解决方案2】:

假设我们有一个数据框列表tables

tables = [df.set_index('From country').copy() for _ in range(10)]

我们将索引设置为'From country',以防它还不是索引。如果已经存在,请跳过该部分。

然后我们将数据帧列表转换为pd.Panel 并在零轴上取平均值

pd.Panel(dict(enumerate(tables))).mean(0)

如果tables 已经是字典,那么我们只需将其直接传递给pd.Panel

pd.Panel(tables).mean(0)

【讨论】:

  • 顺便说一句,我认为重新索引也是必要的,因为如果在某些表中数据较多,在某些表中数据较少,则需要过滤掉不必要的数据。
最近更新 更多