Feb-07-2022, 11:18 PM
Hello,
I am iterating a for loop a data frame by assigning the length of the df to a variable, and iterating the for loop across the range of that variable. This has worked for me, but I copied the code to replicate it but with different raw data / data frame and now I`m getting an error that the index is out of bounds.
I am iterating a for loop a data frame by assigning the length of the df to a variable, and iterating the for loop across the range of that variable. This has worked for me, but I copied the code to replicate it but with different raw data / data frame and now I`m getting an error that the index is out of bounds.
#Create a new empty list for End Date - and populate the list using a if statement nested in a for loop end_date=[] length2=len(kl_50_132_transposed) for i in range(length2): if kl_50_132_transposed.iat[i,2] != kl_50_132_transposed.iat[i-1,2]: end_date.append(kl_50_132_transposed.iat[i+1,3]) else: end_date.append("")This returns the below error text
IndexError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_15288/1266775606.py in <module> 92 for i in range(length2): 93 if kl_50_132_transposed.iat[i,2] != kl_50_132_transposed.iat[i-1,2]: ---> 94 end_date.append(kl_50_132_transposed.iat[i+1,3]) 95 else: 96 end_date.append("") ~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key) 2220 2221 key = self._convert_key(key) -> 2222 return self.obj._get_value(*key, takeable=self._takeable) 2223 2224 def __setitem__(self, key, value): ~\anaconda3\lib\site-packages\pandas\core\frame.py in _get_value(self, index, col, takeable) 3564 if takeable: 3565 series = self._ixs(col, axis=1) -> 3566 return series._values[index] 3567 3568 series = self._get_item_cache(col) IndexError: index 73 is out of bounds for axis 0 with size 73I`ve confirmed that kl_50_132_transposed is a data frame with 73 rows. Any thoughts?