将 pandas 数据框列表连接在一起 [英] Concatenate a list of pandas dataframes together
本文介绍了将 pandas 数据框列表连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据框列表,我想将其合并为一个熊猫数据框.我正在使用Python 2.7.10和Pandas 0.16.2
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)
这将返回数据帧列表
type(dfs[0])
Out[6]: pandas.core.frame.DataFrame
type(dfs)
Out[7]: list
len(dfs)
Out[8]: 408
以下是一些示例数据
# 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]
我想将d1
,d2
和d3
组合到一个熊猫数据框中.另外,使用chunksize
选项时将大表直接读入数据框的方法将非常有帮助.
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.
推荐答案
鉴于所有数据框都具有相同的列,您可以简单地concat
它们:
Given that all the dataframes have the same columns, you can simply concat
them:
import pandas as pd
df = pd.concat(list_of_dataframes)
这篇关于将 pandas 数据框列表连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文