pandas中read_xxx的块读取功能

pandas设计时应该是早就考虑到了这些可能存在的问题,所以在read功能中设计了块读取的功能,也就是不会一次性把所有的数据都放到内存中来,而是分块读到内存中,最后再将块合并到一起,形成一个完整的DataFrame。

def read_sql_table(table_name, con, schema=None, index_col=None,
coerce_float=True, parse_dates=None, columns=None,
chunksize=None):

1.chunksize是在一个每一个chunk块中有多少行。

2.当chunksize是非None的时候read_xxx返回的是一个迭代器

比如我自己的写的一个全量同步数据的代码如下:

gtr=pd.read_sql_table(sync_table, data_from_engine_dict[database],chunksize=20000)
count=0
for df in gtr:
if count==0:
df.to_sql(database+"_"+sync_table, data_to_engine, if_exists="replace", index=False)
else: df.to_sql(database + "_" + sync_table, data_to_engine, if_exists="append", index=False) count+=1

发现数据库中的表会被修改,我今天做了如下升级:

其他的read_xxx也有类似的参数

pandas.read_csv(filepath_or_buffer: Union[str, pathlib.Path, IO[~AnyStr]], sep=',', delimiter=None, header='infer', names=None, index_col=None,
usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None,
skipinitialspace=False, skiprows=None, skipfooter=0, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False,
skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, cache_dates=True,
iterator=False, chunksize=None, compression='infer', thousands=None, decimal=b'.', lineterminator=None, quotechar='"', quoting=0, doublequote=True,
escapechar=None, comment=None, encoding=None, dialect=None, error_bad_lines=True, warn_bad_lines=True, delim_whitespace=False, low_memory=True,
memory_map=False, float_precision=None)[source]

我们再介绍一个不用改的参数:

low_memory : bool, default True
Internally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference.
To ensure no mixed types either set False, or specify the type with the dtype parameter.
Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks.
(Only valid with C parser).
low_memory 默认就是True,如果不小心改成了False,chunksize参数不生效。

最新文章

  1. Spring学习笔记 6. 尚硅谷_佟刚_Spring_Bean 之间的关系
  2. QT 初阶 第二章 创建对话框(查找对话框实例)
  3. ELK日志分析系统搭建(转)
  4. DevExpress DXperience 的本地化(汉化)方法
  5. 【原创】利用C++ RAII技术自动回收堆内存
  6. nginx学习之一
  7. 谨慎使用php的strtotime()函数
  8. QWidget 键盘事件 焦点(源代码级别研究)
  9. Sencha app build 出现 missing name after . operator 问题
  10. javascript如何处理多级的实时监听
  11. jtyhon 介绍
  12. 074、如何安装和配置Rex-Ray?(2019-04-19 周五)
  13. VScode编辑器使用
  14. 包建强的培训课程(4):App测试深入学习和研究
  15. [luogu2446][bzoj2037][SDOI2008]Sue的小球【区间DP】
  16. Intelij的idea和pycharm的使用
  17. Linux网桥配置
  18. centos7安装mysql mariadb
  19. ucos获得系统时间OSTimeGet();
  20. ListBox item Sort

热门文章

  1. 版本管理——Git和SVN的介绍及其优缺点
  2. python学习-45 模块
  3. redis mongodb持久化的方式
  4. 创建新表,自动授权trigger
  5. atomikos 优化JDBC性能
  6. 面试经典算法:快速排序Golang实现
  7. c#获取本月有哪些周六、周日
  8. vs2019 product key
  9. mpeg1、mpeg2和mpeg4标准对比分析和总结
  10. Seaborn(一)之风格管理