Python 从无法放入内存的大数据帧中删除索引

Python 从无法放入内存的大数据帧中删除索引,python,pandas,pandas-datareader,Python,Pandas,Pandas Datareader,我有一个有5000万条记录的文件,我有一个需要从文件中删除的索引列表。如果我想使用pandas dataframe读取文件,我可能会遇到内存问题(如果内存有限)。假设我这样做: df = pd.read_csv('input_file') df = df.drop(df.index[example_ix_list]) df.to_csv('input_file', index=False) 我可能会遇到内存问题: File "/home/ec2-user/CloudMatcher/clou

我有一个有5000万条记录的文件,我有一个需要从文件中删除的索引列表。如果我想使用pandas dataframe读取文件,我可能会遇到内存问题(如果内存有限)。假设我这样做:

df = pd.read_csv('input_file')
df = df.drop(df.index[example_ix_list])
df.to_csv('input_file', index=False)
我可能会遇到内存问题:

  File "/home/ec2-user/CloudMatcher/cloudmatcher/core/execution/user_interaction.py", line 768, in process
    new_unlabel_df = unlabel_df.drop(unlabel_df.index[list_ix])
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/generic.py", line 2162, in drop
    dropped = self.reindex(**{axis_name: new_axis})
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/frame.py", line 2733, in reindex
    **kwargs)
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/generic.py", line 2515, in reindex
    fill_value, copy).__finalize__(self)
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/frame.py", line 2679, in _reindex_axes
    fill_value, limit, tolerance)
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/frame.py", line 2690, in _reindex_index
    allow_dups=False)
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/generic.py", line 2627, in _reindex_with_indexers
    copy=copy)
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/internals.py", line 3897, in reindex_indexer
    for blk in self.blocks]
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/internals.py", line 1046, in take_nd
    allow_fill=True, fill_value=fill_value)
  File "/home/ec2-user/anaconda2/envs/cloudmatch/lib/python2.7/site-packages/pandas/core/algorithms.py", line 1467, in take_nd
    out = np.empty(out_shape, dtype=dtype)
MemoryError
问:我可以使用pandas dataframe将文件分块读取,并使用列表删除索引吗?如果是这样,怎么办?或者有更好的方法让我错过了

非常感谢

试试这个:

pd.read_csv('input_file', skiprows=example_ix_list).to_csv('input_file', index=False)
如果仍然获得MemoryError,则可以使用
chunksize
参数:

example_ix_list = pd.Index(example_ix_list)

for df in pd.read_csv('input_file', chunksize=10**5):
    df.loc[df.index.difference(example_ix_list)] \
      .to_csv('new_file_name', index=False, header=None, mode='a')

您可以将chunk_size参数传递给read_table()或read_csv()命令:

pd.read_csv('fname.csv', sep=',', chunksize=4)

有关更多信息,请参阅。你检查过了吗?

如果我运行第二部分,就会出现此错误。索引器错误:当我以chunksize=10**6运行并且文件有13762668条记录时,索引10006311超出了大小为1000000的轴1的界限。我有什么遗漏吗?谢谢它起作用了。虽然当我计时的时候,它是非常昂贵的。也许是因为不同,但它肯定是有帮助的和工作!太好了。@yguw,您可能想尝试
np.setdiff1d(df.index.values,example_ix_ulist.values)
而不是
df.index.difference(example_ix_ulist)
-这可能会稍微快一点。。。