如何使用一行在 Pandas 中选择范围 [英] How to select range in Pandas using a row

查看:69
本文介绍了如何使用一行在 Pandas 中选择范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Pandas 数据框.

I have a Pandas dataframe.

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

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

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

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.

这是一些伪代码,希望对您有所帮助:

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]

推荐答案

应该这样做

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]

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

这篇关于如何使用一行在 Pandas 中选择范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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