简体   繁体   English

熊猫数据框平均计算

[英]Pandas dataframe average calculation

I have a dataframe that I would like to compute the average across columns. 我有一个数据框,我想计算各列的平均值。 I have the following dataframe: 我有以下数据框:

数据框

Column 'A' repeats but not column 'B'. 列“ A”重复,但列“ B”不重复。 I would like to compute the average of the values in column 'B' for the repeating numbers in column 'A'. 我想计算“ A”列中重复数字的“ B”列中值的平均值。 For example for the first value in column 'A' which is 1 the value in 'B' is 3 and the next value in column 'A' which is 1 the value in 'B' is 9 and the next is 4 and so on. 例如,对于列“ A”中的第一个值是1,“ B”中的值是3,列“ A”中的下一个值是1,“ B”中的值是9,下一个是4,依此类推。 。 Then continue with 2 and 3 etc... 然后继续2和3等...

I was thinking that if I can move those values to columns then compute the average across columns it would be easier but I can't find a way to copy the values there. 我当时在想,如果我可以将这些值移动到列中,然后计算跨列的平均值,会更容易,但是我找不到在其中复制值的方法。 Maybe there is an easier way? 也许有一种更简单的方法?

This is what I would like : 这就是我想要的:

DF2

You can use groupby and mean() 您可以使用groupby和mean()

df.groupby('A').B.mean()

As @fuglede mentioned 正如@fuglede所提到的

df.groupby('A').mean()

would work as well as there is only column B left for aggregation. 可以正常工作,只剩下B列可用于汇总。 Either way you get 无论哪种方式

A
1    6.25
2    6.50
3    4.75

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

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