Converting generator from read_sql in pandas to dataframe has failed

情到浓时终转凉″ 提交于 2021-02-15 05:34:13

问题


I want to read data from my oracle, I use the pandas's read_sql and set the parameter chunksize=20000,

from sqlalchemy import create_engine
import pandas as pd
engine = create_engine("my oracle")
df = pd.read_sql("select clause",engine,chunksize=20000)

It returns a iterator, and I want to convert this generator to a dataframe usingdf = pd.DataFrame(df), but it's wrong, How can the iterator be converted to a dataframe?


回答1:


This iterator can be concatenated, then it return a dataframe:

df = pd.concat(df)

You can view pandas.concat document.

If you can't use concat directly, try the following:

gens = pd.read_sql("select clause",engine,chunksize=20000)
dflist = []
for gen in gens:
    dflist.append(gen)
df = pd.concat(dflist)


来源:https://stackoverflow.com/questions/52144578/converting-generator-from-read-sql-in-pandas-to-dataframe-has-failed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!