【问题标题】:Nested for loop pandas dataframe does not create a new column嵌套for循环熊猫数据框不会创建新列
【发布时间】:2023-03-27 04:30:01
【问题描述】:

我有这个嵌套循环:

rolling=['10','20']

for i in range(len(ex)):
    for d in rolling:
       df = nested_df.loc[other_nested_df['pio'][i]:(other_nested_df['pio'][i]+10)].copy()
       ex.loc[i]['date_vert_'+str(d)=df['date'].iloc[-1]

循环没有给出任何错误,但我不明白为什么最后数据框ex 没有另外两列称为date_vert_10date_vert_20

【问题讨论】:

    标签: python pandas dataframe for-loop nested


    【解决方案1】:

    ex.loc[i][...] 更改为ex.loc[i, ...]

    rolling=['10','20']
    
    for i in range(len(ex)):
        for d in rolling:
           df = nested_df.loc[other_nested_df['pio'][i]:(other_nested_df['pio'][i]+10)].copy()
           ex.loc[i, 'date_vert_'+str(d)=df['date'].iloc[-1]
           # ^^^^^^^^^^^ changed
    

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 2015-11-01
      • 1970-01-01
      • 2015-09-12
      • 2019-01-11
      • 2022-01-08
      • 2017-02-13
      • 2019-03-09
      相关资源
      最近更新 更多