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

Viewed 27

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. However, I got the "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. The iteration works for the same dataframe before resampling.

1 Answers

I just found out what the issue is. After using the set_index method the dataframe has data time indexes. Using range(len (df)) or range(df.size) is not applicable anymore.

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.

Related