加快熊猫CSV阅读和随后的下播速度

  • 本文关键字:速度 熊猫 CSV python pandas
  • 更新时间 :
  • 英文 :


简单的问题 - 我正在执行以下操作:

train_set = pd.read_csv('./input/train_1.csv').fillna(0)
for col in train_set.columns[1:]:
train_set[col] = pd.to_numeric(train_set[col],downcast='integer')

数据帧的第一列是一个字符串 - 其余列是整数。Read_csv提供浮标,我不需要。缩减采样导致使用的 RAM 减少近 50%,但会显著减慢该过程。我可以一步到位地完成整个事情吗?或者有人知道如何多线程吗?
感谢

我建议您尝试这两个函数并再次查看性能:

  1. 读取文件时转换

    # or uint8/int16/int64 depends on your data
    pd.read_csv('input.txt', sep=' ', dtype=np.int32)
    # or you can use converters with lambda function
    pd.read_csv('test.csv', sep=' ', converters={'1':lambda x : int(x)})
    
  2. 读取文件后转换数据帧

    df['MyColumnName'] = df['MyColumnName'].astype(int)

最新更新