V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  zyhuang  ›  全部回复第 1 页 / 共 1 页
回复总数  1
2018-01-12 09:54:56 +08:00
回复了 w12103 创建的主题 Python pandas 代码请教
```
import pandas as pd
import numpy as np

won_last = pd.DataFrame({'lastwin':["lastwin_{}".format(i) for i in range(10000)]})
dset = pd.DataFrame(np.random.randint(10000, size=(10000, 2)),
columns=['home', 'visitor'])
In [2]:
%timeit dset.merge(won_last, left_on='home', right_index=True,how='left').merge(won_last, left_on='visitor', right_index=True,how='left', suffixes="hv")

7.16 ms ± 340 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)


In [3]:
%timeit dset.assign(hlastwin=lambda x: [won_last.lastwin[ii] for ii in x.home], vlastwin=lambda x: [won_last.lastwin[ii] for ii in x.visitor])
355 ms ± 34.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

```
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   914 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 10ms · UTC 20:27 · PVG 04:27 · LAX 12:27 · JFK 15:27
Developed with CodeLauncher
♥ Do have faith in what you're doing.