【发布时间】:2020-07-22 15:29:18
【问题描述】:
我从一个用 Pandas 读取的 Excel 导入。每一行是一个不同的人,列给出了 people_id 等值。
现在我实际上想将每个人放入一个字典,然后将每个字典放入一个列表。但不幸的是,我的循环总是返回同一个人,但在列表中出现了 19 次。错在哪里?
path = os.path.abspath('dataset_people.xlsx') # works as long as file is in same directory as this code. Excel-file must be closed.
excel = pandas.read_excel(path)# excel is now a variable which contains the files contet.
#print(excel) #print whole content.
# set dictionary
people = {}
# set list
list_people = []
# loop dictionary
for index, row in excel.iterrows():
add1 = row['people_id']
add2 = row['timestamp']
add3 = row['floor_departure']
add4 = row['floor_destination']
people['people_id'] = add1
people['timestamp'] = add2
people['floor_departure'] = add3
people['floor_destination'] = add4
# loop list
for index, row in excel.iterrows():
list_people.append(people)
print(list_people)
【问题讨论】:
-
找不到代码有什么问题。你试过
excel.to_dict() -
是的,我做了,似乎有一个值错误:要解压的值太多
标签: pandas list loops dictionary