pandas three-way joining multiple dataframes on columns

前端 未结 11 1897
醉梦人生
醉梦人生 2020-11-22 08:35

I have 3 CSV files. Each has the first column as the (string) names of people, while all the other columns in each dataframe are attributes of that person.

How can

11条回答
  •  無奈伤痛
    2020-11-22 08:57

    Simple Solution:

    If the column names are similar:

     df1.merge(df2,on='col_name').merge(df3,on='col_name')
    

    If the column names are different:

    df1.merge(df2,left_on='col_name1', right_on='col_name2').merge(df3,left_on='col_name1', right_on='col_name3').drop(columns=['col_name2', 'col_name3']).rename(columns={'col_name1':'col_name'})
    

提交回复
热议问题