[英]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.