我有一个Pandas数据帧。
我在另一个进程中从该数据帧中选择了一行。
在另一种方法中,我现在需要从该行所在的数据帧中选择一个范围,如果有那么多,则需要返回55行。
这是一些伪代码,希望它有所帮助:
df = DataFrame from csv
row = df[3454]
index = row.index
start = max(0, index - 55)
end = max(1, index)
dfRange = df[start:end]
答案 0 :(得分:9)
这应该这样做
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]
此链接有更多信息 Getting the integer index of a Pandas DataFrame row fulfilling a condition?