pandas three-way joining multiple dataframes on columns

前端 未结 11 1933
醉梦人生
醉梦人生 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 09:08

    In python 3.6.3 with pandas 0.22.0 you can also use concat as long as you set as index the columns you want to use for the joining

    pd.concat(
        (iDF.set_index('name') for iDF in [df1, df2, df3]),
        axis=1, join='inner'
    ).reset_index()
    

    where df1, df2, and df3 are defined as in John Galt's answer

    import pandas as pd
    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']
    )
    

提交回复
热议问题