Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 执行pandas.merge()时系统冻结_Python_Pandas_Merge - Fatal编程技术网

Python 执行pandas.merge()时系统冻结

Python 执行pandas.merge()时系统冻结,python,pandas,merge,Python,Pandas,Merge,我在4GB内存上运行Win7 64位。我将一个大数据文件(要读取的3Mio行)读入Pandas数据帧,执行几个isin()操作,并获得另外两个数据帧df1和df2,每个数据帧有300000行。在此之前,一切正常,总内存消耗约为40%。然而,当我尝试合并df1和df2时,RAM消耗直接上升到几乎100%,导致系统冻结。看起来像是内存泄漏。有人看到什么东西吗?熊猫屋顶下发生了什么。merge()导致了这种情况?有没有可能让代码运行?“合并”命令: merged=pandas.merge(df1, d

我在4GB内存上运行Win7 64位。我将一个大数据文件(要读取的3Mio行)读入Pandas数据帧,执行几个isin()操作,并获得另外两个数据帧df1和df2,每个数据帧有300000行。在此之前,一切正常,总内存消耗约为40%。然而,当我尝试合并df1和df2时,RAM消耗直接上升到几乎100%,导致系统冻结。看起来像是内存泄漏。有人看到什么东西吗?熊猫屋顶下发生了什么。merge()导致了这种情况?有没有可能让代码运行?“合并”命令:

merged=pandas.merge(df1, df2, on=['call/put','expiration'], how='inner', left_index=True, right_index=True)

您可能需要提供一些关于这方面的示例数据。我怀疑你的加入是多对一还是什么。我尝试建立一个包含6列的示例,我能够在大约2分钟内完成MBP上50000000条记录的连接

我在Ipython笔记本上运行,所以我使用cell magic%%time来计算单元格的运行时间

下面是我的例子:

%%time
np.random.seed(1)
n=50000000 #50,000,000
df1 = pd.DataFrame(randn(n), index=pd.date_range('1/1/2000', periods=n))
df1.columns = ['thing1']
df1['thing2'] = randn(n)
df1['thing3'] = randn(n)
df1['thing4'] = randn(n)
df1['call/put'] = np.random.choice(['put','call'], n)
df1['expiration'] = pd.date_range('1/1/2001', periods=n)

df2 = pd.DataFrame(randn(n), index=pd.date_range('1/1/2000', periods=n))
df2.columns = ['thing1']
df2['thing2'] = randn(n)
df2['thing3'] = randn(n)
df2['thing4'] = randn(n)
df2['call/put'] = np.random.choice(['put','call'], n)
df2['expiration'] = pd.date_range('1/1/2001', periods=n)
在我的箱子上大约需要40秒

print df1.head()
print df2.head()

             thing1    thing2    thing3    thing4 call/put expiration
2000-01-01  1.624345 -1.139160 -1.226383 -0.157804     call 2001-01-01
2000-01-02 -0.611756 -0.082128 -0.982924  0.254592     call 2001-01-02
2000-01-03 -0.528172 -1.601699  0.457530 -0.671379      put 2001-01-03
2000-01-04 -1.072969  0.496285 -1.747807  0.181793      put 2001-01-04
2000-01-05  0.865408 -1.481422 -0.435733  1.582169     call 2001-01-05
              thing1    thing2    thing3    thing4 call/put expiration
2000-01-01 -0.020954  0.054025  2.502060  1.011011     call 2001-01-01
2000-01-02  0.635003 -1.757002 -0.311092  1.469307     call 2001-01-02
2000-01-03  1.547721  0.267789 -2.703976  0.671766      put 2001-01-03
2000-01-04 -1.288127 -0.745521  0.614661  0.897899     call 2001-01-04
2000-01-05  0.094685 -0.451766 -0.012700 -0.641612      put 2001-01-05
然后我进行合并:

%%time
merged=pd.merge(df1, df2, on=['call/put','expiration'], how='inner', left_index=True, right_index=True)

CPU times: user 20.3 s, sys: 19 s, total: 39.3 s
Wall time: 1min 58s

那么,您的真实数据和这里的虚拟示例之间可能有什么不同呢?

当df1中有N条记录与df2中的M条记录匹配时,它会生成N x M条记录,显示为系统慢度。我怀疑您的问题是“on”参数缺少某些列-stroke/symbol等。只有部分列位于on[]内。不包括4列。两个框架的维度都相同,大约300000行x 8列,包括索引。当我用5000万行运行您的示例时,创建df1需要12分钟,创建df2需要36分钟,然后有一条消息:内核似乎意外死亡。使用“重新启动内核”继续使用此控制台。尝试合并会导致内存错误。我在4 GB的WIn7上运行。你呢?我使用的是8GB内存的MBP,但有一个win虚拟机使用>2GB运行。你真的应该考虑升级你的RAM。16GB内存低于200美元(假设您在美国),MBP是多少?我仍然想知道pandas.merge为什么会导致内存错误,因为这个过程应该很慢,但不会导致错误。您是在Linux还是Windows上运行Python?MBP=MacBookPro。所以我在Mac电脑上运行。有时在Linux中,您运行的是64位版本的Python还是32位版本的Python?