python对excel某一列求和-Python / Excel:将给定值与求和值进行比较

1586010002-jmsa.png

From "Excel Image 1", I am pulling data into python of Column A and Column B. The goal is to sum the values of Column B, and see whether it is higher or lower than the given sum of 1500. If it is higher or equal to 1500, then there needs to be nothing done. But, if it is lower, then adjust the values that has the word "change" beside them, such that the calculated sum becomes 1500 or more. In our case, the sum is 700, which is lower than 1500.

import xlrd

excel = '/Users/Bob/Desktop/'

wb1 = xlrd.open_workbook(excel + 'assignment2.xlsx')

sh1 = wb1.sheet_by_index(0)

colA,colB = [],[]

for a in range(3,sh1.nrows):

colA.append(int(sh1.cell(a,0).value))

colB.append(int(sh1.cell(a,1).value))

print(colA)

print(colB)

excel_sum=(sh1.cell_value(2,1))

print("Given Sum:", excel_sum)

calc_sum = sum(colB)

print(calc_sum)

if calc_sum >= excel_sum:

print("Good")

#else:

#Need to adjust the values that has the words "change" beside them

Current Output:

[0, 1, 2, 3, 4]

[900, -400, -200, 300, 100]

Given Sum: 1500.0

700

Thanks!

解决方案

You know the delta - excel_sum - calc_sum. So scan column C for a change, and add it to column B.

else:

for row in range(3, sh1.nrows):

if sh1.cell(row, 2) == 'change':

colB[row] = int(sh1.cell(row, 1)) + excel_sum - calc_sum

break

Of course, I see you using xlrd but not xlwt, so I don't know how you're going to change the value. Do you?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值