简体   繁体   中英

Appending a list of dataframes to a list of dataframe in python

I was hoping to discover an easy solution but I have not been able to come across one. Lets say I have a two lists of 4 dataframes each. Each item of the list is a dataframe. One list is called

list_of_df1

the other is called

list_of_df2

I am wondering if there is away to append the dataframes from one list into the other list. The end goal is to have one combined list with all 8 dataframes. I am sorry I did not provided sample data. I am hoping it is an easy line of code.

This is what I tried:

list_of_df1.append(list_of_df2)
list_of_df1.insert(list_of_df2)

Thank you!

考虑到list_of_df1list_of_df2是列表:

list_of_df1.extend(list_of_df2)

As simple as:

list_of_df1 += list_of_df2

This is actually extremely slightly more efficient than using extend() , since it doesn't include the overhead of a function call.

Try pd.concat(frames)

import pandas as pd

d1 = {'ID': [10,20,40,50], 'Name': ["Bob", "John","test","Foo"]}   
d2 = {'ID': [10,20,40,50], 'Name': ["Bob", "John","test","Foo"]}   
df1 = pd.DataFrame(data=d1)
df2 = pd.DataFrame(data=d2)

frames = [df1, df2]
result = pd.concat(frames)
print(result)

Output:

ID  Name
 10   Bob
 20  John
 40  test
 50   Foo
 10   Bob
 20  John
 40  test
 50   Foo

If you have list of DFs, then the solution posted by @ruohola is the one that you are looking for.

The starred expression remove the brackets of the list.

[*list_df1, *list_df2]

Probably not the most efficient though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM