如何在循环中动态设置数据框值

时间:2019-06-23 00:19:07

标签: python pandas dataframe

感谢您调查我的问题! 综上所述,我循环运行代码(“从2019年到2028年为年份”),在每次迭代中,我从另一个仅随年份变化的数据框中获得一个恒定值“温度”。 我正在尝试基于此修改名为“预测”的列的值。例如,在第一次迭代中,当年份为2019时,我希望预报=温度,只更改year = 2019的行,以此类推,接下来的年份

我尝试使用if来设置条件,但是有错误。

for year in range (2019,2029):
    temperature=(
        df_Ckt.loc[df_Ckt['Circuit Key']==circuit,year].reset_index(drop=True))
    if df['year'] == year:
       df['forecast_year']=year_value

错误如下:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我知道我可以避免像loc这样的函数使用if-else,但是loc是否只返回数据帧中的值而不是常量值?

0 个答案:

没有答案