【问题标题】:Remove rows if do exist in another dataframe - python pandas如果在另一个数据框中确实存在,则删除行 - python pandas
【发布时间】:2021-09-21 00:25:19
【问题描述】:

我有两个大型数据集:

  • 训练大小:289816 行 X 689 列

  • 测试大小:49863 行 X 689 列

我想删除一些测试集行,因为它们已经存在于训练中。

我检查了以下答案https://stackoverflow.com/a/44706892

但不幸的是,由于 144 Gb 的内存被填满,python 进程被杀死。

有没有更好的不消耗资源的解决方案?

【问题讨论】:

标签: python pandas merge


【解决方案1】:

我会推荐一个分段解决方案,类似于以下伪代码:

chunksize = 10**5

for test_chunk in pd.read_csv(test_set_path, delimiter=';', dtype=str, chunksize=chunksize):
    
    # In this loop all the elements that were in testing are filtered
    for train_chunk in pd.read_csv(train_set_path, delimiter=';', dtype=str, chunksize=chunksize):
        test_chunk = test_chunk.loc[~train_chunk.index]

    test_chunk.loc.to_csv('path/to/your/result/csv', mode='a', index=False)

这样你就不会内存不足...

【讨论】:

    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2013-06-24
    相关资源
    最近更新 更多