Pandas:获取平均数据帧

2024-04-26 01:15:55 发布

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

我有50个数据帧和一个结构,但值是不同的。如何从这些数据中获得平均数据帧?你知道吗

       active nodes
graph              
0               128
1               128
2               128
3               127
4               126
5               126
6               126
7               126
8               126
9               125
10              124

Tags: 数据结构graphactivenodes
2条回答

将所有数据帧添加到一个列表中,连接它们并计算每行的平均值:

dfs = [df1, df2, ... dfn]
pd.concat(dfs, axis=1).mean(axis=1)

改用numpy。你知道吗

假设数据帧列表dfs

dfs = [pd.DataFrame(np.random.randint(10, size=(10, 10))) for _ in range(50)]

然后用np.concatenate计算平均值,然后取mean。但是作为numpy这也应该更快。你知道吗

pd.Series(np.concatenate([df.values for df in dfs], axis=1).mean(1), dfs[0].index)

0    4.472
1    4.722
2    4.644
3    4.574
4    4.624
5    4.446
6    4.548
7    4.606
8    4.440
9    4.442
dtype: float64

计时

enter image description here

相关问题 更多 >

    热门问题