python - Pandas 获得列平均值/平均值

标签 python pandas

我无法获得 pandas 中列的平均值或平均值。 A 有一个数据框。我在下面尝试的任何事情都没有给我列 weight

的平均值
>>> allDF 
         ID           birthyear  weight
0        619040       1962       0.1231231
1        600161       1963       0.981742
2      25602033       1963       1.3123124     
3        624870       1987       0.94212

以下返回几个值,而不是一个:

allDF[['weight']].mean(axis=1)

也是这样:

allDF.groupby('weight').mean()

最佳答案

如果您只想要 weight 列的平均值,请选择该列(这是一个系列)并调用 .mean():

In [479]: df
Out[479]: 
         ID  birthyear    weight
0    619040       1962  0.123123
1    600161       1963  0.981742
2  25602033       1963  1.312312
3    624870       1987  0.942120

In [480]: df["weight"].mean()
Out[480]: 0.83982437500000007

关于python - Pandas 获得列平均值/平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31037298/

相关文章:

python - 如何拆分数据框单元格中的数据并在拆分时执行 Pandas groupby?

Python装饰器函数调用深度,结构化输出

Python Gevent共享队列(监听进程)

python - python中n的函数从n的列表中选择k个元素

python - 如何处理通过将isbnlib.meta与 Pandas 一起应用返回的错误

python - 修改描述函数以删除和添加列

python - 如果值落在某个范围内,则根据另一列的条件创建新列

python - Pandas GroupBy 日期范围取决于每一行

python - 如何滚动 Tix ScrolledWindow

python - 在MDCard Kivy中添加ScrollView