Concatenate a list of pandas dataframes together

前端 未结 4 885
北恋
北恋 2020-11-29 19:44

I have a list of Pandas dataframes that I would like to combine into one Pandas dataframe. I am using Python 2.7.10 and Pandas 0.16.2

I created the list of datafram

相关标签:
4条回答
  • 2020-11-29 20:01

    concat also works nicely with a list comprehension pulled using the "loc" command against an existing dataframe

    df = pd.read_csv('./data.csv') # ie; Dataframe pulled from csv file with a "userID" column
    
    review_ids = ['1','2','3'] # ie; ID values to grab from DataFrame
    
    # Gets rows in df where IDs match in the userID column and combines them 
    
    dfa = pd.concat([df.loc[df['userID'] == x] for x in review_ids])
    
    0 讨论(0)
  • 2020-11-29 20:02

    You also can do it with functional programming:

    from functools import reduce
    reduce(lambda df1, df2: df1.merge(df2, "outer"), mydfs)
    
    0 讨论(0)
  • 2020-11-29 20:08

    If the dataframes DO NOT all have the same columns try the following:

    df = pd.DataFrame.from_dict(map(dict,df_list))
    
    0 讨论(0)
  • 2020-11-29 20:12

    Given that all the dataframes have the same columns, you can simply concat them:

    import pandas as pd
    df = pd.concat(list_of_dataframes)
    
    0 讨论(0)
提交回复
热议问题