我有一个庞大的pandas数据帧,具有以下结构:
df1:
A B
0 0 12
1 0 15
2 0 17
3 0 18
4 1 45
5 1 78
6 1 96
7 1 32
8 2 45
9 2 78
10 2 44
11 2 10
Run Code Online (Sandbox Code Playgroud)
第二个,像这样小:
df2
G H
0 0 15
1 1 45
2 2 31
Run Code Online (Sandbox Code Playgroud)
我想按照以下规则向我的第一个数据帧添加一列: column df1.C = df2.H when df1.A == df2.G
我设法用for循环来做,但数据库很大,代码运行得非常慢,所以我正在寻找一个Pandas-way或numpy来做它.
非常感谢,
鲍里斯
我想知道是否有可能直接在Cython代码中(即在中)遍历地图.pyx。这是我的示例:
import cython
cimport cython
from licpp.map import map as mapcpp
def it_through_map(dict mymap_of_int_int):
# python dict to map
cdef mapcpp[int,int] mymap_in = mymap_of_int_int
cdef mapcpp[int,int].iterator it = mymap_in.begin()
while(it != mymap.end()):
# let's pretend here I just want to print the key and the value
print(it.first) # Not working
print(it.second) # Not working
it ++ # Not working
Run Code Online (Sandbox Code Playgroud)
这不会编译: Object of type 'iterator' has no attribute 'first'
我之前在cpp中使用了地图容器,但是对于此代码,我试图坚持使用cython / python,这可能吗?
DavidW解决的 这是在DavidW回答之后的代码的有效版本:
import …Run Code Online (Sandbox Code Playgroud)