有一个字典
dict_example = {
'Request_1': {
'request_id' : '1',
'name' : 'Foo'
},
'Request_2': {
'request_id' : '2',
'name' : 'Bar'
},
'Request_3': {
'request_id' : '3',
'name' : 'Barbie'
}
然后我通过此dict通过迭代发出API请求,每个请求都转换为一个数据框,结果存储在列表响应中。
API_request = get_me_api(
for k,v in dict_example.items():
name=v['name'])
responses.append(API_request)
responses = [df1, df2, df3]
df1
age name city street
0 1 Foo LA street A
df2
age name city street
0 10 Bar NY street B
df3
age name city street
0 20 Barbi SF street C
我想向每个数据框添加附加列'request_id'。
我尝试进行迭代
for v in yt_params.values():
dict_example ['request_id'] = v['request_id']
# and just a list
request_ids = [1,2,3]
for response in responses:
for request in request_ids:
response['request_id'] = request
但是它会为每个数据帧创建一个始终带有最后一个request_id的列
df1
age name city street request_id
0 1 Foo LA street A 3
df2
age name city street request_id
0 10 Bar NY street B 3
df3
age name city street request_id
0 20 Barbi SF street C 3
答案 0 :(得分:1)
您可能需要<input type="checkbox" name="test" title="Test" required>
例如:
zip
答案 1 :(得分:1)
您在循环中犯了一个错误。 request_id
将始终以request_ids
中的最后一个条目结尾。这是正在发生的事的一个示例:
letters = ["a", "b", "c"]
numbers = [1, 2, 3]
end_product = {}
for letter in letters:
for number in numbers:
end_product[letter] = number
print(end_product)
输出:
{'a': 1}
{'a': 2}
{'a': 3} #Last iteration sets "a" to 3
{'a': 3, 'b': 1}
{'a': 3, 'b': 2}
{'a': 3, 'b': 3} # Last iteration sets "b" to 3
{'a': 3, 'b': 3, 'c': 1}
{'a': 3, 'b': 3, 'c': 2}
{'a': 3, 'b': 3, 'c': 3} # Last iteration sets "c" to 3