如何传入 pandas' 迭代器列表作为 zip 的参数?

How to pass in a list of pandas' iterators as the argument for zip?

我正在阅读五个巨大的 CVS 文件。它们都具有相同的行数,但行数以百万计。由于内存限制,我需要分批读取它们,然后将来自不同文件的数据连接到一个数据帧中。

下面是我现在的:

import pandas as pd
it1 = pd.read_csv('1.csv', chunksize=10)
it2 = pd.read_csv('2.csv', chunksize=10)

it3 it4 it5 在列表 list_iterators 中给出。即:

list_iterators = [it3  it4  it5]

我想实现的是,每当我执行一个读操作时,我会以列表的形式从所有迭代器中获取数据。

所以我第一次阅读它们时,我会有:

[first 10 rows in 1.csv, first 10 rows in 2.csv, first 10 rows in 3.csv ...  first 10 rows in 5.csv]

为了达到预期的效果,我现在做的是:

ak = zip(it1, it2, list_iterators[0], list_iterators[1], list_iterators[2])
ak.__next__() #I will call this to read the next 10 rows

我想知道是否有任何方法可以将 list_iterators 作为参数传递,而不是拼写出其中的所有元素,因为我无法知道 [ 中有多少元素=16=] 当我写程序的时候。

我的第二个问题是,除了使用 __next__(),是否有更优雅的方法从 pandas 迭代器中检索数据。

I wonder if there is any way that I can pass the list_iterators as an argument

是的,您可以使用 * 运算符传递 list_iterators 的内容:

ak = zip(it1, it2, *list_iterators)