将生成器从 Pandas 中的 read_sql 转换为数据帧失败 [英] Converting generator from read_sql in pandas to dataframe has failed

查看:52
本文介绍了将生成器从 Pandas 中的 read_sql 转换为数据帧失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的oracle读取数据,我使用pandas的read_sql并设置参数chunksize=20000

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)

它返回一个迭代器,我想使用df = pd.DataFrame(df)将这个生成器转换为数据帧,但是错误,迭代器如何转换为数据帧?

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?

推荐答案

这个迭代器可以连接起来,然后返回一个数据帧:

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

df = pd.concat(df)

您可以查看pandas.concat 文档.

如果不能直接使用concat,请尝试以下操作:

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)

这篇关于将生成器从 Pandas 中的 read_sql 转换为数据帧失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆