简体   繁体   English

通过索引标准对Pandas数据框切片进行索引

[英]Pandas dataframe slice indexing by index criterias

I often need to do some searching in the index by some higher/lower than criteria in Pandas DataFrame. 我经常需要以比Pandas DataFrame中的条件高/低的标准在索引中进行一些搜索。 I have found a way to do it, but it feels a bit cumbersome, or somehow unsmart. 我已经找到了一种方法,但是感觉有点麻烦,或者有点不明智。 This is my current method: 这是我目前的方法:

from numpy import linspace
import pandas as pd

df = pd.DataFrame(linspace(1,5,5),index=linspace(0.1,0.5,5))

df

     0
0.1  1
0.2  2
0.3  3
0.4  4
0.5  5

df[(df.index>0.3) * (df.index <0.5)]

     0
0.3  3
0.4  4

It does give me what I wan't, but please suggest a better way if you have one. 它确实给了我我所不想要的东西,但是如果有的话,请提出一种更好的方法。

I would do it like this. 我会这样做。 Operating with a float-like index is a bit unusual and can give somewhat unexpected results. 使用类似浮点的索引进行操作有点不寻常,并且可能会产生一些意想不到的结果。 In 0.13 (release very shortly), it has much more support, but still operates differently that a 'regular' index. 在0.13(即将发布)中,它具有更多的支持,但运行方式仍与“常规”索引不同。 See here 这里

In [4]: df = pd.DataFrame({ 'A' : np.linspace(1,5,5), 'B' : np.linspace(0.1,0.5,5) })

In [5]: df
Out[5]: 
   A    B
0  1  0.1
1  2  0.2
2  3  0.3
3  4  0.4
4  5  0.5

In [6]: df.loc[(df.B>0.3)&(df.B<0.5)]
Out[6]: 
   A    B
2  3  0.3
3  4  0.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM