对于数据框 A 中的每一行,我需要查询 DF B。我需要执行以下操作:按列 b1 (B.b1) 中的值过滤 B 行,这些值在列 A.a1 和 A.a2 定义的范围内并将组合值分配给 A.a3 列。
在 pandas 中会是这样的:
A.a1 = B[(B.b1>A.a2) & (B.b1<A.a3)]['b2'].values
我尝试在 UDF 的函数参数中传递数据框,但出现错误:
ValueError: Cannot determine Numba type of <class 'cudf.core.dataframe.DataFrame'>
下面是一个使用 Pandas 的工作 Python 示例。
toyevents = pd.DataFrame.from_dict({'end': {0: 8.748356416,
1: 8.752231441000001,
2: 8.756627850000001,
3: 8.760818359,
4: 8.765967569,
5: 8.77041589,
6: 8.774226174,
7: 8.776358813,
8: 8.77866835,
9: 8.780719302000001},
'name_id': {0: 18452.0,
1: 20586.0,
2: 20491.0,
3: 20610.0,
4: 20589.0,
5: 20589.0,
6: 19165.0,
7: 20589.0,
8: 20586.0,
9: 19064.0},
'start': {0: 8.748299848,
1: 8.752229263,
2: 8.756596980000001,
3: 8.760816603,
4: 8.765957310000001,
5: 8.770381615,
6: 8.77414259,
7: 8.776349745000001,
8: 8.778666861000001,
9: 8.780674982}})
toynvtx = pd.DataFrame.from_dict({'NvtxEvent.Text': {0: 'Iteration 32',
1: 'FWD pass',
2: 'Prediction and loss',
3: 'BWD pass',
4: 'Optimizer update'},
'end': {0: 8.802574018000001,
1: 8.771325765,
2: 8.771688249,
3: 8.792846429,
4: 8.802333183},
'start': {0: 8.744061385,
1: 8.747272157000001,
2: 8.771329333,
3: 8.771691628000001,
4: 8.792851876}})
# Search NVTX ranges encompassing [start,end] range.
def pickNVTX(r,nvtx):
start = r['start']
end = r['end']
start_early = nvtx[nvtx['start'] <= start]
end_later = start_early[start_early['end'] >= end]
return ','.join(end_later['NvtxEvent.Text'])
# Using apply()
toyevents.loc[:,'nvtx'] = toyevents_.apply(pickNVTX,nvtx=toynvtx,axis=1)
# Method 2. Using iterrows()
for i, row in toyevents.iterrows():
toyevents.loc[i, 'nvtx'] = ','.join(
toynvtx[(toynvtx.start <= row.start)
& (toynvtx.end >= row.end)]['NvtxEvent.Text'].values)