作为参考,这一切都在PyCharm Educational Edition 1.0.1中的Windows 7 x64位机器上,使用Python 3.4.2和Pandas 0.16.1
我有〜791MB .csv文件,大约304万行x24列。该文件包含2014年1月至2015年2月爱荷华州的酒类销售数据。如果您对此感兴趣,可以在此处找到该文件:https://data.iowa.gov/Economy/Iowa-Liquor-Sales/m3tr-qhgy。
其中一个标题为商店位置的列保存包含纬度和经度的地址。以下程序的目的是将纬度和经度从商店位置单元格中取出,并将每个单元格放置在自己的单元格中。当文件减少到~104万行时,我的程序正常工作。
1 import pandas as pd
2
3 #import the original file
4 sales = pd.read_csv('Iowa_Liquor_Sales.csv', header=0)
5
6 #transfer the copies into lists
7 lat = sales['STORE LOCATION']
8 lon = sales['STORE LOCATION']
9
10 #separate the latitude and longitude from each cell into their own list
11 hold = [i.split('(', 1)[1] for i in lat]
12 lat2 = [i.split(',', 1)[0] for i in hold]
13 lon2 = [i.split(',', 1)[1] for i in hold]
14 lon2 = [i.split(')', 1)[0] for i in lon2]
15
16 #put the now separate latitude and longitude back into their own columns
17 sales['LATITUDE'] = lat2
18 sales['LONGITUDE'] = lon2
19
20 #drop the store location column
21 sales = sales.drop(['STORE LOCATION'], axis=1)
22
23 #export the new panda data frame into a new file
24 sales.to_csv('liquor_data2.csv')
但是,当我尝试使用完整的3.04行文件运行代码时,它会给我这个错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1595, in drop
dropped = self.reindex(**{axis_name: new_axis})
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 2505, in reindex
**kwargs)
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1751, in reindex
self._consolidate_inplace()
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 2132, in _consolidate_inplace
self._data = self._protect_consolidate(f)
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 2125, in _protect_consolidate
result = f()
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 2131, in <lambda>
f = lambda: self._data.consolidate()
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 2833, in consolidate
bm._consolidate_inplace()
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 2838, in _consolidate_inplace
self.blocks = tuple(_consolidate(self.blocks))
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 3817, in _consolidate
_can_consolidate=_can_consolidate)
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 3840, in _merge_blocks
new_values = _vstack([b.values for b in blocks], dtype)
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 3870, in _vstack
return np.vstack(to_stack)
File "C:\Python34\lib\site-packages\numpy\core\shape_base.py", line 228, in vstack
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
MemoryError
我尝试在python控制台中逐行运行代码,发现程序运行sales = sales.drop后出现错误([&#39; STORE LOCATION&#39;],axis = 1)线。
我在其他地方搜索过类似的问题,我提出的唯一答案就是在程序读取文件时对文件进行分块,如下所示:
#import the original file
df = pd.read_csv('Iowa_Liquor_Sales7.csv', header=0, chunksize=chunksize)
sales = pd.concat(df, ignore_index=True)
我唯一的问题就是我得到了这个错误:
Traceback (most recent call last):
File "C:/Users/Aaron/PycharmProjects/DATA/Liquor_Reasign_Pd.py", line 14, in <module>
lat = sales['STORE LOCATION']
TypeError: 'TextFileReader' object is not subscriptable
我的google-foo都很棒。谁知道该怎么办?
更新 我应该使用分块方法指定,当程序尝试复制商店位置列时会出现错误。
答案 0 :(得分:3)
所以我找到了问题的答案。我在python 2.7而不是python 3.4中运行程序。我做的唯一更改是删除第8行,因为它未使用。我不知道2.7是否只是以不同的方式处理内存问题,或者我是否在3.4中未正确安装了pandas软件包。我将在3.4中重新安装pandas以查看是否存在问题,但如果其他人有类似问题,请在2.7中尝试您的程序。
UPDATE 意识到我在64位机器上运行32位python。我升级了我的python版本,它现在运行时没有内存错误。