I have a dataframe with columns SRi
and SAi
. Each row represents a different location and these columns contain arrays of SRi
and SAi
for each location. I want to find the index of the maximum value of SAi
and then find the corresponding value of SRi
for that index, w
. Here is a screenshot of my dataframe, called AvgT
I'm able to find the index of the max value of SAi
for each location (row) with the following loop but can't find the corresponding value at the same index in SRi
. I get the error "IndexError: index out of bounds". I think what I have written is finding the value at the index of SRi
rather than the index of each array within SRi
. How can I fix this so that it finds the index of each array within SRi
for each location?
MaxSA=[]
for index, row in AvgT.iterrows():
maxsa=np.argmax(AvgT.SAi[index])
w=AvgT.SRi[maxsa]
MaxSA.append(maxsa)
CodePudding user response:
This line isn't doing what you think it's doing:
w=AvgT.SRi[maxsa]
You are accessing the value of SRi in row maxsa of the dataframe -- that is, you are getting the whole list. I assume you are getting an IndexError because in at least one instance, the argmax of SAi is higher than the number of rows in your dataframe.
Try replacing that line with this:
w=AvgT.SRi[index][maxsa]