pandas three-way joining multiple dataframes on columns

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

    Assumed imports:

    import pandas as pd
    

    John Galt's answer is basically a reduce operation. If I have more than a handful of dataframes, I'd put them in a list like this (generated via list comprehensions or loops or whatnot):

    dfs = [df0, df1, df2, dfN]
    

    Assuming they have some common column, like name in your example, I'd do the following:

    df_final = reduce(lambda left,right: pd.merge(left,right,on='name'), dfs)
    

    That way, your code should work with whatever number of dataframes you want to merge.

    Edit August 1, 2016: For those using Python 3: reduce has been moved into functools. So to use this function, you'll first need to import that module:

    from functools import reduce
    

提交回复
热议问题