[英]Python Pandas DataFrame: KeyError 0 while iterating through the DataFrame after using the set_index and resample methods
在使用 pandas set_index 方法并将 15 分钟 dataframe 重新采样到 1 分钟 dataframe 后,我试图用一个数据帧迭代数据索引。 但是,我得到了“KeyError:0”。
我刚刚尝试了以下简单的代码行,但它不起作用:
for i in range(df.size):
print(df[i,"value"])
我也试过这个:
for i in range(len(df)):
print(df[i,"value"])
我注意到这个错误只发生在我使用 set.index 和重采样方法时。 在重新采样之前,迭代适用于相同的 dataframe。
我刚刚发现问题是什么。 使用 set_index 方法后,dataframe 具有数据时间索引。 使用 range(len (df)) 或 range(df.size) 不再适用。
因此我应该使用一系列时间索引来遍历它或重置日期时间索引并将默认索引作为索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.