我需要在没有舍入误差的情况下对pandas中的列进行乘法(保持总数相同)。你知道吗
所以我有一个数据帧(称为combined\ u df),它看起来像:
| areaid | districtid | percent | home | job |
| 89012 | 55 | 1.0 | 70 | 20 |
| 123048 | 442 | 0.984496| 100 | 10 |
| 123048 | 34536 | 0.015504| 100 | 10 |
areaid
- smaller area inside a city
- for example in areaid 123048: 100 people are residents and 10 people work
districtid
- larger area inside a city
- for exampleareaid
is inside two districts 442 and 34536
我需要计算有多少人是居民,并在每个地区的工作(结果应该是一个整数)。我们可以假设人们均匀地分布在每个区域内,所以只需将percent列与home/job列相乘,然后按districtid
列进行分组。你知道吗
我所做的:
def count_people(percent, people):
return np.around(percent * people)
result = pd.DataFrame()
result['districtid'] = combined_df['districtid']
result['area_district_home'] = count_people(combined_df['percent'], combined_df['home'])
result['area_district_job'] = count_people(combined_df['percent'], combined_df['job'])
# total residents:
total_home = sum(result.groupby('districtid')['area_district_home'].sum())
但是,如果我把所有的居民加起来,他们将不等于areaid
中的所有居民。我想这是由于舍入误差造成的。误差很小(1900万人口为17个百分点)。你知道吗
有没有办法更准确地计算每个地区的居民和工人人数?在这一点上,我不知道为什么会有这个舍入误差,因为如果0.984496*100将被舍入到98,那么0.015504*100应该被舍入到2,并且总和将相等。你知道吗
Python有一个内置的round()函数,它接受两个数值参数n和ndigits,并返回n取整为ndigits的数字。ndigits参数默认为零,因此不使用它将导致数字四舍五入为整数。正如您将看到的,round()可能并不像您期望的那样工作。你知道吗
相关问题 更多 >
编程相关推荐