【问题标题】:Merge a dataframe from dictionary with for loop将字典中的数据框与 for 循环合并
【发布时间】:2021-02-10 23:51:55
【问题描述】:

我有一个包含数据帧的字典 (new_dict) 和带有日期的数据帧 (serie_tiempo),我想从 new_dict 中的每个数据帧中选择一列并将其添加到 dates索引是一样的。 filelist 中是字典中每个数据框的名称

filelist
aux1=list(range(23))

for i in aux1:
   serie= pd.merge(serie_tiempo,new_dict[filelist[i]][[filelist[i]]], on="time_stamp", how='left')

for 循环中的行添加我需要的列,但不保留之前添加的列。 我需要对我的 for 循环进行一些修复并完成。你能帮帮我吗?

【问题讨论】:

    标签: python pandas dataframe dictionary for-loop


    【解决方案1】:

    merge 函数返回合并的对象。您需要在循环中使用相同的对象。

    试试这个代码:

    for i in aux1:
       serie_tiempo = pd.merge(serie_tiempo,new_dict[filelist[i]][[filelist[i]]], on="time_stamp", how='left')
    
    print(serie_tiempo)
    

    【讨论】:

      猜你喜欢
      • 2021-08-03
      • 2022-11-17
      • 2023-03-13
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2021-04-03
      • 2019-03-23
      相关资源
      最近更新 更多