Python 如何使用一行在 Pandas 中选择范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25681579/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:29:52  来源:igfitidea点击:

How to select range in Pandas using a row

pythonpandas

提问by Tjorriemorrie

I have a Pandas dataframe.

我有一个 Pandas 数据框。

I have in another process selected a row from that dataframe.

我在另一个过程中从该数据框中选择了一行。

At another method, I now need to select a range from that dataframe where the row is and going back 55 rows, if there is so many.

在另一种方法中,我现在需要从该行所在的数据框中选择一个范围,如果有这么多行,则返回 55 行。

Here is some pseudo code, hope it helps:

下面是一些伪代码,希望对你有帮助:

df = DataFrame from csv

row = df[3454]

index = row.index
start = max(0, index - 55)
end = max(1, index)
dfRange = df[start:end]

回答by user1827356

This should do it

这应该做

integer_location = np.where(df.index == 3454)[0][0]
start = max(0, integer_location - 55)
end = max(1, integer_location)
dfRange = df.iloc[start:end]

This link has more info Getting the integer index of a Pandas DataFrame row fulfilling a condition?

此链接包含更多信息 获取满足条件的 Pandas DataFrame 行的整数索引?