【问题标题】:How to connect two pandas data frames from right and left?如何从左右连接两个熊猫数据框?
【发布时间】:2013-08-14 21:42:08
【问题描述】:

有没有办法以更优雅的方式(即使用更少的命令)执行以下操作:

df_1 = pandas.DataFrame({'col1':[1,2,3], 'col2':[10,20,30]})
df_2 = pandas.DataFrame({'col3':[100,200,300], 'col4':[1000,2000,3000]})

for col in ['col3','col4']:
    df_1[col] = df_2[col]

print df_1

【问题讨论】:

    标签: merge append pandas dataframe


    【解决方案1】:

    您可以使用concat

    In [407]: pd.concat([df_1, df_2], axis=1)
    Out[407]: 
       col1  col2  col3  col4
    0     1    10   100  1000
    1     2    20   200  2000
    2     3    30   300  3000
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2020-12-18
      • 2021-10-16
      • 2013-06-18
      • 2018-09-13
      • 1970-01-01
      • 2022-10-06
      • 2018-04-08
      • 2016-03-13
      相关资源
      最近更新 更多