我有一个很大的 CSV 文件(磁盘上约 50 GB),但无法立即将其完全读入内存。数据集本身是面板数据,看起来像
ID Time Col 1 ... Col N
1 2000/1/1 ...
1 2000/1/2
...
2 2000/1/1 ...
...
我加载这些数据的想法是以块的形式读取它,进行一些预处理以减少大小,然后单独保存每个块。我知道使用pd.read_csv(..., chunksize=1000)
这让我可以循环大小为 1000 的块,但为了使预处理准确,我更愿意循环遍历与 ID 列相对应的块。 (对应于特定的所有行ID
需要进行准确的预处理)
换句话说,假设我有一个较小的文件,其中包含所有ID
值(例如 1-1000)。然后,我想做一些类似的事情
list_of_id_chunks = [ [1,2,3], [4,5,6], [7,8,9], ... ] # Split the total IDs into chunks of 3 IDs each
for chunk_of_ids in list_of_id_chunks:
# 1. Read the large csv file with only the rows where `ID` is in chunk_of_ids
# (For the first iteration, this should have rows with ID = 1, 2, or 3)
# 2. Do some preprocessing to trim file size
# 3. Save files in csv, feather, etc
有什么建议么?