【问题标题】:Adding dataframe columns onto another dataframe将数据框列添加到另一个数据框
【发布时间】:2015-04-09 14:27:47
【问题描述】:

我有以下两个数据框:

df1:

Symbol, Open, High, Low, Close
abc,    123,  676,  100, 343

df2:

Symbol, Target1, Target2
abc,    654,     565

我正在尝试根据符号组合这两个数据帧,即:必须将 Target1/Target2 添加为新列:

Symbol, Open, High, Low, Close, Target1, Target2
abc,    123,  676,  100, 343,   654,     565

我尝试了一些加入/合并的想法,但似乎无法让它发挥作用。

请有人建议。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    只需concat 他们并传递参数axis=1

    In [7]:
    
    pd.concat([df,df1], axis=1)
    Out[7]:
      Symbol  Open  High  Low  Close Symbol  Target1  Target2
    0    abc   123   676  100    343    abc      654      565
    

    merge 在“符号”列中:

    In [8]:
    
    df.merge(df1, on='Symbol')
    Out[8]:
      Symbol  Open  High  Low  Close  Target1  Target2
    0    abc   123   676  100    343      654      565
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多