2

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!

4 Answers 4

5

Considering list_of_df1 and list_of_df2 are lists:

list_of_df1.extend(list_of_df2)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This code works but I will be using the solution with just adding the two lists together.
4

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.

3 Comments

I think this will be valid if he wants one dataframe that has the contents of all the dataframes in the list, but he is saying that he wants a list of all dataframes.
Okay let me update the answer. THanks. I thought he is saying list means list of data inside a dataframe.
This doesn't do what the OP asks.
3

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.

2 Comments

Thank you! This is exactly the easy solution I was looking for!
@Jake Glad I could help! :)
1

The starred expression remove the brackets of the list.

[*list_df1, *list_df2]

Probably not the most efficient though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.