pandas three-way joining multiple dataframes on columns

前端 未结 11 1769
醉梦人生
醉梦人生 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:51

    You could try this if you have 3 dataframes

    # Merge multiple dataframes
    df1 = pd.DataFrame(np.array([
        ['a', 5, 9],
        ['b', 4, 61],
        ['c', 24, 9]]),
        columns=['name', 'attr11', 'attr12'])
    df2 = pd.DataFrame(np.array([
        ['a', 5, 19],
        ['b', 14, 16],
        ['c', 4, 9]]),
        columns=['name', 'attr21', 'attr22'])
    df3 = pd.DataFrame(np.array([
        ['a', 15, 49],
        ['b', 4, 36],
        ['c', 14, 9]]),
        columns=['name', 'attr31', 'attr32'])
    
    pd.merge(pd.merge(df1,df2,on='name'),df3,on='name')
    

    alternatively, as mentioned by cwharland

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

提交回复
热议问题