Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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 dataframes from:

import pandas as pd

dfs = []

sqlall = "select * from mytable"

for chunk in pd.read_sql_query(sqlall , cnxn, chunksize=10000):

    dfs.append(chunk)

This returns a list of dataframes

type(dfs[0])

Out[6]: pandas.core.frame.DataFrame

type(dfs)

Out[7]: list

len(dfs)

Out[8]: 408

Here is some sample data

# sample dataframes

d1 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]})

d2 = pd.DataFrame({'one' : [5., 6., 7., 8.], 'two' : [9., 10., 11., 12.]})

d3 = pd.DataFrame({'one' : [15., 16., 17., 18.], 'two' : [19., 10., 11., 12.]})

# list of dataframes

mydfs = [d1, d2, d3]

I would like to combine d1, d2, and d3 into one pandas dataframe. Alternatively, a method of reading a large-ish table directly into a dataframe when using the chunksize option would be very helpful.

1 Answer

0 votes
by (41.4k points)

 Since all the

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

import pandas as pd

df = pd.concat(list_of_dataframes)

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

 

Related questions

Browse Categories

...