Python 以矢量化方式查找另一个数据帧中包含值子集的行
如何匹配来自此数据帧的值Python 以矢量化方式查找另一个数据帧中包含值子集的行,python,pandas,Python,Pandas,如何匹配来自此数据帧的值源: car_id lat lon 0 100 10.0 15.0 1 100 12.0 10.0 2 100 09.0 08.0 3 110 23.0 12.0 4 110 18.0 32.0 5 110 21.0 16.0 5 110 12.0 02.0 并仅保
源:
car_id lat lon
0 100 10.0 15.0
1 100 12.0 10.0
2 100 09.0 08.0
3 110 23.0 12.0
4 110 18.0 32.0
5 110 21.0 16.0
5 110 12.0 02.0
并仅保留其坐标位于第二个数据帧中的坐标
坐标
:
lat lon
0 12.0 10.0
1 23.0 12.0
3 18.0 32.0
因此,生成的数据帧结果为:
car_id lat lon
1 100 12.0 10.0
3 110 23.0 12.0
4 110 18.0 32.0
我可以用apply
以迭代的方式来实现这一点,但我正在寻找一种矢量化的方式。我使用isin()
尝试了以下操作,但没有成功:
result = source[source[['lat', 'lon']].isin({
'lat': coords['lat'],
'lon': coords['lon']
})]
上述方法返回:
ValueError: ('operands could not be broadcast together with shapes (53103,) (53103,2)
默认情况下,合并具有相同名称的所有列(两个DFs的列的交点):
这里有一种方法-
样本运行-
In [74]: source
Out[74]:
car_id lat lon
0 100 10.0 15.0
1 100 12.0 10.0
2 100 9.0 8.0
3 110 23.0 12.0
4 110 18.0 32.0
5 110 21.0 16.0
5 110 12.0 2.0
In [75]: coords
Out[75]:
lat lon
0 12.0 10.0
1 23.0 12.0
3 18.0 32.0
In [76]: a = source.values
...: b = coords.values
...:
In [77]: source[(a[:,1:]==b[:,None]).all(-1).any(0)]
Out[77]:
car_id lat lon
1 100 12.0 10.0
3 110 23.0 12.0
4 110 18.0 32.0
a = source.values
b = coords.values
out = source[(a[:,1:]==b[:,None]).all(-1).any(0)]
In [74]: source
Out[74]:
car_id lat lon
0 100 10.0 15.0
1 100 12.0 10.0
2 100 9.0 8.0
3 110 23.0 12.0
4 110 18.0 32.0
5 110 21.0 16.0
5 110 12.0 2.0
In [75]: coords
Out[75]:
lat lon
0 12.0 10.0
1 23.0 12.0
3 18.0 32.0
In [76]: a = source.values
...: b = coords.values
...:
In [77]: source[(a[:,1:]==b[:,None]).all(-1).any(0)]
Out[77]:
car_id lat lon
1 100 12.0 10.0
3 110 23.0 12.0
4 110 18.0 32.0