“可渗透"在数据帧切片中返回索引的方法 [英] "Pandorable" way to return index in dataframe slicing

查看:74
本文介绍了“可渗透"在数据帧切片中返回索引的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种可取的方法来仅获取数据帧切片中的索引? 换句话说,是否有更好的方法来编写以下代码:

Is there a pandorable way to get only the index in dataframe slicing? In other words, is there a better way to write the following code:

df.loc [df ['A']> 5] .index

df.loc[df['A'] >5].index

谢谢!

推荐答案

是的,最好只过滤索引值,而不是所有DataFrame,然后选择索引:

Yes, better is filter only index values, not all DataFrame and then select index:

#filter index
df.index[df['A'] >5]

#filter DataFrame
df[df['A'] >5].index

性能上也存在差异:

np.random.seed(1245)
df = pd.DataFrame({'A':np.random.randint(10, size=1000)})
print (df)

In [40]: %timeit df.index[df['A'] >5]
208 µs ± 11.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [41]: %timeit df[df['A'] >5].index
428 µs ± 6.42 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [42]: %timeit df.loc[df['A'] >5].index
466 µs ± 40.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)


如果性能很重要,请使用numpy-通过 values 到numpy数组:


If performance is important use numpy - convert values of index and column by values to numpy array:

In [43]: %timeit df.index.values[df['A'] >5]
157 µs ± 8.71 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [44]: %timeit df.index.values[df['A'].values >5]
8.91 µs ± 196 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

这篇关于“可渗透"在数据帧切片中返回索引的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆