Reputation: 474
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!
Upvotes: 2
Views: 2414
Reputation: 11
The starred expression remove the brackets of the list.
[*list_df1, *list_df2]
Probably not the most efficient though.
Upvotes: 1
Reputation: 69755
Considering list_of_df1
and list_of_df2
are lists:
list_of_df1.extend(list_of_df2)
Upvotes: 5
Reputation: 4684
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.
Upvotes: 4
Reputation: 24038
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.
Upvotes: 3