Python 2.7 如何使用字典更新数据帧中的一个值?
在执行另一个计算之前,我不会基于字典的值更新数据帧中的单个值 我正在使用一个数据帧,希望计算每行不同列的值。一行的计算完成后,需要在进行新计算之前更改其后行中的值。这是因为之后的行中的值取决于前一行的结果 为了接近它,我正在使用一本字典。目前,我正在Excel中工作,在Excel中,我可以使用字典更新单个单元格的值。然而,为了加快计算速度,我希望使用适当的数据帧 我设法根据结果更新字典,但我没有设法用这些新值更新数据框 适用于基于Excel的当前模型的代码为:Python 2.7 如何使用字典更新数据帧中的一个值?,python-2.7,dataframe,dictionary,Python 2.7,Dataframe,Dictionary,在执行另一个计算之前,我不会基于字典的值更新数据帧中的单个值 我正在使用一个数据帧,希望计算每行不同列的值。一行的计算完成后,需要在进行新计算之前更改其后行中的值。这是因为之后的行中的值取决于前一行的结果 为了接近它,我正在使用一本字典。目前,我正在Excel中工作,在Excel中,我可以使用字典更新单个单元格的值。然而,为了加快计算速度,我希望使用适当的数据帧 我设法根据结果更新字典,但我没有设法用这些新值更新数据框 适用于基于Excel的当前模型的代码为: dict1={1:10,2:15.
dict1={1:10,2:15.....38:29} #my dictionary
for row in range(2,sheet.max_row+1):
#updates the table with values of the dictionary before each calculation
sheet['F'+str(row)].value = dict1[sheet['C'+str(row)].value]
# calculations being executed
(.....)
#updating the dictionary with the results of the calculations in the row
dict1_1={sheet['C'+str(row)].value :sheet['F'+str(row)].value}
dict1.update(dict1_1)
到目前为止,我使用数据帧所做的尝试如下所示:
for row in df.T.itertuples():
df.replace({"P_building_kg_y": dict1}) ##### <-----HERE IS THE PROBLEM!
# calculations being executed
(.....)
#updating the dictionary with the results of the calculations in the row
dict1_1=dict(zip(df.FacilityID, df.P_building_kg_y))
dict1.update(dict1_1)
df.T.itertuples()中的行的:
根据df.replace()
:将to中给出的值替换为值,因此我认为您需要传递列表(dict1.values())
,而不仅仅是dict1
。根据df.replace()
:将to中给出的值替换为值,所以我认为您需要传递list(dict1.values())
或者不仅仅是dict1
。