简体   繁体   English

Python dataframe:计算所有列的平均值/差异/总和/…

[英]Python dataframe: calculating average/diff/sum/… over all columns

I have a large DataFrame (ie thousands of rows and 20 columns) and I want to calculate the average (or any other mathmathical function like the total sum etc) over all columns.我有一个大的 DataFrame (即数千行和 20 列),我想计算所有列的平均值(或任何其他数学 function 等)。 example:例子:

x = [
 [0.5 0.7 0.1 4 80 101],
 [0.1 0.7 0.8 5 4 58],
 [0.4 0.1 0.6 6 1 66],
  ...
 [0.9 0.4 0.1 7 44 12]
]

This should result in这应该导致

avg = [0.475 0.95 ...]

or或者

sum = [15.1 8.17 ...]

Is there any quick formula or oneliner that can easily apply this formula?是否有任何快速公式或oneliner可以轻松应用此公式? It does not have to be a pandas.DataFrame, a Numpy array is also good它不一定是 pandas.DataFrame,一个 Numpy 阵列也不错

df.mean(axis=0)
df.sum(axis=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM