How can I merge together several pandas dataframes on a certain column without 'pandas.merge'?

前端 未结 2 391
星月不相逢
星月不相逢 2021-01-27 13:15

I often find myself with several pandas dataframes in the following form:

import pandas as pd
df1 = pd.read_table(\'filename1.dat\')
df2 = pd.read_table(\'filen         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-27 14:04

    You can set columnA as the index and concat (reset index at the end):

    dfs = [df1, df2, df3]
    
    pd.concat([df.set_index('columnA') for df in dfs], axis=1).reset_index()
    Out: 
      columnA  first_values  second_values  third_values
    0   name1           342              8           910
    1   name2           822              1           301
    2   name3           121              1           132
    3   name4          3434              2           299
    

提交回复
热议问题