python-2.7 - 如何使用字典更新数据框中的一个值?
问题描述
在执行另一个计算之前,我无法根据字典的值更新数据框中的单个值
我正在使用数据框,并希望计算每行不同列中的值。在完成一行的计算后,需要更改之后行中的值,然后才能进行新的计算。这是因为此后行中的值取决于前一行的结果。
为了接近它,我正在使用字典。目前我在 Excel 中工作,我设法用字典更新单个单元格的值。但是,为了使计算更快,我想使用适当的数据框。
我设法根据结果更新字典,但我没有设法用这些新的 citionary 值更新我的数据框
适用于我当前基于 Excel 的模型的代码是:
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)
我只想根据字典更新数据框中的值。如果您知道如何做到这一点,我将不胜感激!
解决方案
推荐阅读
- python - 是否可以一次性分配给 numpy 数组(arr[a:b,c:d])的 2 个不同范围?
- php - 在文件 /usr/webconfig/modules/libphp7.so 中找不到 API 模块结构 `php7_module'
- ios - 如何在新的 swift 文件中通过 swift 覆盖延迟加载 dataArr?
- regex - 带引号的 Kibana 模糊搜索
- java - h2o MOJO 预测与 h2o.predict 对于 GBM 都不同
- python - 在终端中,我将使用 vi 命令在目录中创建一个文件:`vi bot.py`(在执行 mkdir Supreme_bot 之后)我如何在 cmd 上执行此操作?
- rest - 通过 API 上传文件是否需要通过 FTP 访问服务器?
- next.js - 在 next.config.js 中访问请求对象
- shell - AZ CLI (az vm run-command invoke) 使用参数执行时丢失可变数据
- ios - 为我杀死应用程序时可以清除推送通知吗?