使用pandas创建平均数据帧

2024-05-19 13:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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或stack exchange中的其他地方找不到任何函数可以像我正在尝试的那样平均每个值,如何处理这个问题

附言:在一些表格中,或多或少有一些国家


Tags: thefrom国家countryunitedkingdomfrancegermany
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)

enter image description here

可以先将^{}与参数sheetname=None一起用于Dataframesdict。然后通过^{}创建大df,通过第二级index创建^{},并聚合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

如果有多个coutry,最后使用^{}通过引用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

相关问题 更多 >