Python 从 excel 文件读取和计算数据时出现问题
Python issue with reading and calculating data from excel file
I have attached a screenshot of the excel file I am working with
我正在尝试读取此 excel 文件,其中包含所有州(B 列)、县(C 列)和人口(D 列)。我想计算每个州的人口。
我知道我们可以通过多种方式做到这一点,而且肯定有一种方法可以用更少的代码行来做到这一点,而且代码行数少且易于理解。我将不胜感激,但我也想知道如何按照我的想法来做到这一点——首先找出唯一的州名称,然后遍历 sheet 以按州添加所有列。
这是我的代码:
x = wb.get_sheet_names()
sheet = wb.get_sheet_by_name('Population by Census Tract')
PopData = {}
StateData = []
i = 3
j = 0
k=""
#First value entered
StateData.append(sheet['B' + str(2)].value)
#Unique State Values calculated
for row in range(i, sheet.max_row + 1):
if any(sheet['B' + str(row)].value in s for s in StateData):
i=i+1
else:
StateData.append(sheet['B' + str(row)].value)
print(StateData)
#Each State's Population calculated
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[s]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
print(PopData)
我收到这个错误:
if sheet['B' + str(row)].value == StateData[s]:
TypeError: list indices must be integers or slices, not str
在下面:
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[s]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
s
已经是 StateData
列表的一个元素。你想做的大概是:
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == s:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
或
for i, s in enumerate(StateData):
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[i]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
但第一个替代方案更优雅并且(可能)稍微快一些。
I have attached a screenshot of the excel file I am working with 我正在尝试读取此 excel 文件,其中包含所有州(B 列)、县(C 列)和人口(D 列)。我想计算每个州的人口。
我知道我们可以通过多种方式做到这一点,而且肯定有一种方法可以用更少的代码行来做到这一点,而且代码行数少且易于理解。我将不胜感激,但我也想知道如何按照我的想法来做到这一点——首先找出唯一的州名称,然后遍历 sheet 以按州添加所有列。
这是我的代码:
x = wb.get_sheet_names()
sheet = wb.get_sheet_by_name('Population by Census Tract')
PopData = {}
StateData = []
i = 3
j = 0
k=""
#First value entered
StateData.append(sheet['B' + str(2)].value)
#Unique State Values calculated
for row in range(i, sheet.max_row + 1):
if any(sheet['B' + str(row)].value in s for s in StateData):
i=i+1
else:
StateData.append(sheet['B' + str(row)].value)
print(StateData)
#Each State's Population calculated
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[s]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
print(PopData)
我收到这个错误:
if sheet['B' + str(row)].value == StateData[s]:
TypeError: list indices must be integers or slices, not str
在下面:
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[s]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
s
已经是 StateData
列表的一个元素。你想做的大概是:
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == s:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
或
for i, s in enumerate(StateData):
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[i]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
但第一个替代方案更优雅并且(可能)稍微快一些。