python数据框架中均值的计算

2024-03-28 13:22:46 发布

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

我只需要计算数据集中某个特定变量的[平均值]。 我在python中有一个数据帧,包含如下变量 身份证,姓名,一月,二月,三月,四月,六月。 使用这个变量,我必须创建一个新变量,比如“Avgmnth”。仅计算2月、3月、4月的平均值

怎么计算这个Python。救命我在这里


Tags: 数据平均值姓名身份证avgmnth
1条回答
网友
1楼 · 发布于 2024-03-28 13:22:46

下次尝试使用搜索功能。 如果我没弄错你的问题,你在找this?你知道吗

正如@Herms所回答的

If your reduce is already returning your sum, then all you have left to do is divide.

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l) / len(l)

though sum(l)/len(l) would be simpler, as you wouldn't need a lambda.

If you want a more exact float result instead of an int then just use float(len(l)) instead of len(l).

相关问题 更多 >