根据 pandas 中其他列的值计算捐款总额
Calculate sum of donation money based on value in other column in pandas
我正在尝试计算来自房地产行业的竞选捐款数据的货币总和。
realestate_counter = 0
realestate_donations = 0
for row in range(df.shape[0]): #for each row in the dataframe
if 'realestate' in df.iloc[row]['Occupation']:
realestate_donations = realestate_donations + int(row)
print(df.iloc[row]['Amount'])
realestate_counter = realestate_counter + 1
# print('-------')
print('$' + str(realestate_donations) +' was doanted from the real estate industry.')
print('There are ' + str(realestate_counter) +' Real Estate donors.')
我的数据框有多种职业,但我只对房地产感兴趣。代码块打印职业为 realestate
的捐赠值并尝试对其求和。
我将 print(df.iloc[row]['Amount])
打印的数字粘贴到 Google sheet 中并在那里求和。
该金额与这行代码计算的金额大不相同ealestate_donations = realestate_donations + int(row)
Google sheets: 72200
我的代码:23973
我怀疑我的代码哪里出了问题,Google sheet 是准确的。
我已经复制了下面的整个打印输出:
500.0
200.0
1000.0
1000.0
1000.0
1000.0
100.0
50.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
-1000.0
1000.0
-1000.0
-1000.0
1000.0
1000.0
1000.0
1000.0
500.0
300.0
100.0
1000.0
1000.0
1000.0
1000.0
100.0
250.0
100.0
250.0
250.0
500.0
1000.0
973 was doanted from the real estate industry.
There are 88 Real Estate donors.
这是一个精简的示例,但应该能满足您的需求
df = pd.DataFrame({'occupation':['real estate','real estate','real estate','banker','baker'], 'donation':range(11,16)})
occupation donation
0 real estate 11
1 real estate 12
2 real estate 13
3 banker 14
4 baker 15
# filter on occupation
df[df['occupation']=='real estate']
occupation donation
0 real estate 11
1 real estate 12
2 real estate 13
# sum of all donations
df[df['occupation']=='real estate']['donation'].sum()
36
# count of all donations
df[df['occupation']=='real estate']['donation'].count()
3
Groupby 比较
df.groupby('occupation').sum()
donation
occupation
baker 15
banker 14
real estate 36
df.groupby('occupation').count()
donation
occupation
baker 1
banker 1
real estate 3
我正在尝试计算来自房地产行业的竞选捐款数据的货币总和。
realestate_counter = 0
realestate_donations = 0
for row in range(df.shape[0]): #for each row in the dataframe
if 'realestate' in df.iloc[row]['Occupation']:
realestate_donations = realestate_donations + int(row)
print(df.iloc[row]['Amount'])
realestate_counter = realestate_counter + 1
# print('-------')
print('$' + str(realestate_donations) +' was doanted from the real estate industry.')
print('There are ' + str(realestate_counter) +' Real Estate donors.')
我的数据框有多种职业,但我只对房地产感兴趣。代码块打印职业为 realestate
的捐赠值并尝试对其求和。
我将 print(df.iloc[row]['Amount])
打印的数字粘贴到 Google sheet 中并在那里求和。
该金额与这行代码计算的金额大不相同ealestate_donations = realestate_donations + int(row)
Google sheets: 72200
我的代码:23973
我怀疑我的代码哪里出了问题,Google sheet 是准确的。
我已经复制了下面的整个打印输出:
500.0
200.0
1000.0
1000.0
1000.0
1000.0
100.0
50.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
1000.0
-1000.0
1000.0
-1000.0
-1000.0
1000.0
1000.0
1000.0
1000.0
500.0
300.0
100.0
1000.0
1000.0
1000.0
1000.0
100.0
250.0
100.0
250.0
250.0
500.0
1000.0
973 was doanted from the real estate industry.
There are 88 Real Estate donors.
这是一个精简的示例,但应该能满足您的需求
df = pd.DataFrame({'occupation':['real estate','real estate','real estate','banker','baker'], 'donation':range(11,16)})
occupation donation
0 real estate 11
1 real estate 12
2 real estate 13
3 banker 14
4 baker 15
# filter on occupation
df[df['occupation']=='real estate']
occupation donation
0 real estate 11
1 real estate 12
2 real estate 13
# sum of all donations
df[df['occupation']=='real estate']['donation'].sum()
36
# count of all donations
df[df['occupation']=='real estate']['donation'].count()
3
Groupby 比较
df.groupby('occupation').sum()
donation
occupation
baker 15
banker 14
real estate 36
df.groupby('occupation').count()
donation
occupation
baker 1
banker 1
real estate 3