简体   繁体   English

Python Pandas DataFrame: KeyError 0 while iterate through the ZBA834BA059A9A379459C1112E4Z1 using after ZBA834BA059A9A379459C1112E71

[英]Python Pandas DataFrame: KeyError 0 while iterating through the DataFrame after using the set_index and resample methods

After using the pandas set_index method and resampling the 15min dataframe to 1min dataframe I was trying to iterate through the dataframe- so the one with the 1min data time indexes.在使用 pandas set_index 方法并将 15 分钟 dataframe 重新采样到 1 分钟 dataframe 后,我试图用一个数据帧迭代数据索引。 However, I got the "KeyError: 0".但是,我得到了“KeyError:0”。

I just tried the following simple code lines but it does not work:我刚刚尝试了以下简单的代码行,但它不起作用:

for i in range(df.size):
    print(df[i,"value"])

I tried also this one:我也试过这个:

for i in range(len(df)):
    print(df[i,"value"])

I noticed that this error only occurs when I use the set.index and the resampling method.我注意到这个错误只发生在我使用 set.index 和重采样方法时。 The iteration works for the same dataframe before resampling.在重新采样之前,迭代适用于相同的 dataframe。

I just found out what the issue is.我刚刚发现问题是什么。 After using the set_index method the dataframe has data time indexes.使用 set_index 方法后,dataframe 具有数据时间索引。 Using range(len (df)) or range(df.size) is not applicable anymore.使用 range(len (df)) 或 range(df.size) 不再适用。

Therefore I should use a range of time indexes to iterate through it or reset the date time indexes and make default indexes as index.因此我应该使用一系列时间索引来遍历它重置日期时间索引并将默认索引作为索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM