我试图迭代一个日期框并获得某些行之间的最大列数,问题是当我将索引值放在我得到的nan上时:
for index, row in df.iterrows():
if index >= 51:
print(df.loc[index:(index - 51), 'close'].max())
为此,我得到一个nan值。
但如果我在切片器中使用如下数字:
for index, row in df.iterrows():
if index >= 51:
print(df.loc[0:51, 'close'].max())
我会得到的结果不是我需要的,因为我需要它是一个移动的窗口,但这只是为了显示问题
你知道为什么它不接受索引作为有效的切片器吗
我认为问题在于
loc
索引片向后,导致没有返回任何内容;在第一次迭代中,您的切片是df.loc[51:0, 'close'].max()
。相反:我假设索引是整数/数字,因此为什么可以使用
loc
混合整数和列名?否则我认为iloc
可以工作修正案:这是我用
iloc
所想的,但(在我看来)与你的方法没有什么不同:相关问题 更多 >
编程相关推荐