Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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 3.x 在数据框中搜索列表中每个元素的最有效方法_Python 3.x_Pandas_Numpy_Dataframe_Data Science - Fatal编程技术网

Python 3.x 在数据框中搜索列表中每个元素的最有效方法

Python 3.x 在数据框中搜索列表中每个元素的最有效方法,python-3.x,pandas,numpy,dataframe,data-science,Python 3.x,Pandas,Numpy,Dataframe,Data Science,我有一个超过100万的数据集,比如d。我需要找到像seekingframe这样的数据框架的索引,它在该数据集中超过1500个元素 import pandas as pd d=pd.DataFrame([225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320]) seekingframe=pd.DataFrame([275,280,285,290,295,300,305,310,315,32

我有一个超过100万的数据集,比如d。我需要找到像seekingframe这样的数据框架的索引,它在该数据集中超过1500个元素

import pandas as pd 

d=pd.DataFrame([225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320])
seekingframe=pd.DataFrame([275,280,285,290,295,300,305,310,315,320,325,330,335,340,345,350,355,180,255,260])
我需要尽快在d中找到seekingframe的每个元素。我的意思是,我需要一个最终数组,如:

array([ 10, 11,  12, 13, 14, 15, 16, 17, 18, 19, -1, -1, -1, -1, -1, -1, -1, -1, 6, 7])
或者像数组一样的差异

[11, 12, 13, 14, 15, 16, 17, 18]

表示相似或不同的事物。实际上,如果可能的话,我宁愿放弃不同的设置。

使用numpy可能会更快。在这些小的唯一数组上,numpy比pandas快100倍以上。isin没有将假定_unique=True传递给numpy函数,该函数查找两个数组np.in1d的交集并返回True或False

如果您通过了假设_unique=True,则速度会快300倍:


使用numpy可能会更快。在这些小的唯一数组上,numpy比pandas快100倍以上。isin没有将假定_unique=True传递给numpy函数,该函数查找两个数组np.in1d的交集并返回True或False

如果您通过了假设_unique=True,则速度会快300倍:


每个数据帧中的数字都是唯一的吗?这些数字是严格单调的吗?这似乎也是一个numpy问题,因此标记是否正确。每个数据帧中的数字都是唯一的吗?这些数字是严格单调的吗?这似乎也是一个numpy问题,因此标记是否正确。
#finding similar
%timeit d[d[0].isin(seekingframe[0])].index
404 µs ± 6.25 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

#finding difference
%timeit seekingframe[~seekingframe[0].isin(d[0])].index
458 µs ± 2.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

# finding similar with numpy arrays and NOT passing `assume_unique=True`
a = d[0].to_numpy()
b = seekingframe[0].to_numpy()
%timeit np.arange(a.shape[0])[np.in1d(a, b)]

35.4 µs ± 779 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

# finding similar with numpy arrays and passing `assume_unique=True`
a = d[0].to_numpy()
b = seekingframe[0].to_numpy()
%timeit np.arange(a.shape[0])[np.in1d(a, b, assume_unique=True)]

12 µs ± 337 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)