Pandas.DataFrame 按索引间隔选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28244141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas.DataFrame select by interval of indexes
提问by phasselmann
I would like to know, in a pythonic way, how could I select elements in the Pandas.Dataframe inside a given interval in their indexes. Basically I wish to know if there is a command like pandas.Series.between for DataFrame.index .
我想知道,以 Pythonic 的方式,如何在索引的给定间隔内选择 Pandas.Dataframe 中的元素。基本上我想知道 DataFrame.index 是否有像 pandas.Series.between 这样的命令。
example:
例子:
df1 = pd.DataFrame(x, index=(1,2,...,100000000), columns=['A','B','C'])
df2 = df1.between(start=10, stop=100000)
df1 = pd.DataFrame(x, index=(1,2,...,100000000), columns=['A','B','C'])
df2 = df1.between(开始=10,停止=100000)
I think it is curious not easily finding anything related to this.
我认为很难找到与此相关的任何内容。
回答by EdChum
You can just use the subscript notation with locwhich is label based indexing:
您可以只使用loc基于标签的索引的下标符号:
In [3]:
df2 = df1.loc[10:100000]
df2
Out[3]:
A B C
10 NaN NaN NaN
11 NaN NaN NaN
12 NaN NaN NaN
13 NaN NaN NaN
14 NaN NaN NaN
15 NaN NaN NaN
.....
99994 NaN NaN NaN
99995 NaN NaN NaN
99996 NaN NaN NaN
99997 NaN NaN NaN
99998 NaN NaN NaN
99999 NaN NaN NaN
10000 NaN NaN NaN
[99991 rows x 3 columns]
You also mention not being able to find documentation about this but it's pretty easy to find and clear: http://pandas.pydata.org/pandas-docs/stable/indexing.html
您还提到无法找到有关此的文档,但很容易找到和清除:http: //pandas.pydata.org/pandas-docs/stable/indexing.html

