如果我有一个太大而无法用 pandas 加载到内存中的 csv 文件(在本例中为 35gb),我知道可以使用 chunksize 分块处理该文件。
但是我想知道是否可以根据列中的值更改块大小。
我有一个 ID 列,然后每个 ID 都有几行包含信息,如下所示:
ID, Time, x, y
sasd, 10:12, 1, 3
sasd, 10:14, 1, 4
sasd, 10:32, 1, 2
cgfb, 10:02, 1, 6
cgfb, 10:13, 1, 3
aenr, 11:54, 2, 5
tory, 10:27, 1, 3
tory, 10:48, 3, 5
ect...
我不想将 ID 分成不同的块。例如,将处理大小为 4 的块:
ID, Time, x, y
sasd, 10:12, 1, 3
sasd, 10:14, 1, 4
sasd, 10:32, 1, 2
cgfb, 10:02, 1, 6
cgfb, 10:13, 1, 3 <--this extra line is included in the 4 chunk
ID, Time, x, y
aenr, 11:54, 2, 5
tory, 10:27, 1, 3
tory, 10:48, 3, 5
...
是否可以?
如果没有,也许使用带有 for 循环的 csv 库,如下所示:
for line in file:
x += 1
if x > 1000000 and curid != line[0]:
break
curid = line[0]
#code to append line to a dataframe
尽管我知道这只会创建一个块,并且 for 循环需要很长时间来处理。