我试图了解 Pandas 中iloc函数的执行复杂度是多少。我阅读了以下 Stack Exchange 线程(Pandas DataFrame 搜索是线性时间还是常数时间?):
“按索引访问单行(索引已排序且唯一)应具有运行时 O(m) 其中
m << n_rows”
提到按时iloc运行O(m)。什么是m(线性,对数,常数,...)?
我运行的一些实验:
import pandas as pd
>>> a = pd.DataFrame([[1,2,3],[1,3,4],[2,3,4],[2,4,5]], columns=['a','b','c'])
>>> a = a.set_index('a').sort_index()
>>> a
b c
a
1 3 4
1 4 5
2 2 3
2 3 4
>>> a.iloc[[0,1,2,3]]
b c
a
1 3 4
1 4 5
2 2 3
2 3 4
Run Code Online (Sandbox Code Playgroud)
所以iloc显然适用于偏移量而不是基于整数的索引(列a)。即使我们删除顶部的几行,iloc …