我是靠谱客的博主 无辜小兔子,这篇文章主要介绍python如何读取数据框中的数据,如何在Python数据框中读取大块数据?,现在分享给大家,希望可以做个参考。

I want to read the file f in chunks to a dataframe. Here is part of a code that I used.

for i in range(0, maxline, chunksize):

df = pandas.read_csv(f,sep=',', nrows=chunksize, skiprows=i)

df.to_sql(member, engine, if_exists='append',index= False, index_label=None, chunksize=chunksize)

I get the error:

pandas.io.common.EmptyDataError: No columns to parse from file

The code works only when the chunksize >= maxline (which is total lines in file f). However, in my case, the chunksize<=maxline.

Please advise the fix.

解决方案

I think it is better to use the parameter chunksize in read_csv. Also, use concat with the parameter ignore_index, because of the need to avoid duplicates in index:

chunksize = 5

TextFileReader = pd.read_csv(f, chunksize=chunksize)

df = pd.concat(TextFileReader, ignore_index=True)

See pandas docs.

最后

以上就是无辜小兔子最近收集整理的关于python如何读取数据框中的数据,如何在Python数据框中读取大块数据?的全部内容,更多相关python如何读取数据框中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部