当使用块读取大型 csv 文件时,如何在连接块之前处理除一列以外的所有列

how to process all but one column before concatenating chunks when use chunks to read large csv file

我有一个很大的 csv 文件 (7GB),我使用这些代码在 Pandas:

中读取它
chunks=pd.read_table('input_filename', chunksize=500000)
df=pd.DataFrame()
df=pd.concat((chunk==1) for chunk in chunks)

这对我有用,因为文件是单热编码的,所以 chunk==1 部分将 0 和 1 转换为布尔值,这为我节省了一些内存使用量。

现在我想用同样的方法读入另一个文件,唯一的问题是新文件有一个 ID 列,它不是单热编码的。我的问题是:如何保持 ID 列不变并以相同的方式转换其余列?

我尝试了一些子集技术,包括:

df=pd.concat((chunk.loc[:, -1]==1) for chunk in chunks)

但其中 none 到目前为止有效。

谢谢!

试试这个:

chunks = pd.read_csv('input_filename', chunksize=500000, index_col='ID')
df = pd.concat([chunk.astype(bool) for chunk in chunks]).reset_index()