2024-03-28 13:22:46 发布
网友
我只需要计算数据集中某个特定变量的[平均值]。 我在python中有一个数据帧,包含如下变量 身份证,姓名,一月,二月,三月,四月,六月。 使用这个变量,我必须创建一个新变量,比如“Avgmnth”。仅计算2月、3月、4月的平均值
怎么计算这个Python。救命我在这里
下次尝试使用搜索功能。 如果我没弄错你的问题,你在找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).
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.
sum(l)/len(l)
If you want a more exact float result instead of an int then just use float(len(l)) instead of len(l).
float(len(l))
len(l)
下次尝试使用搜索功能。 如果我没弄错你的问题,你在找this?你知道吗
正如@Herms所回答的
相关问题 更多 >
编程相关推荐