Search code examples
pythondataframesklearn-pandasindex-error

How to prevent IndexError of pandas series in a nested loop?


I have a nested loop. In the outer loop, I iterate through MORE keys than the dataframe[key]s in the inner loop. That causes an IndexError (key cannot be found in dataframe[key]. I need a way to check if the keys match.

f.e. just the key columns:

dfKeys                    dataframe
-------                   --------
 key                         key
  1                           1
  1                           3
  3                           5
  3                           9
  3
  4
  4
  5
  5
  5
  5
  8
  8
  9
  9
  9 

grouped=dfKeys.groupby('key')
for key, group in grouped:
    if ((group.someCol=='someVal').any() or ~(group.someCol.isin(someArray).any())):
        if(key in dataframe['key']): #######this did not solve it, always false (but there have to be some matches) 
            foundIndex=dataframe[dataframe['key']==key].index.values.astype(int)[0] #INDEXERROR after some loops if there is no 'if' above
            dataframe.loc[foundIndex,'myCol']='myVal'

Type of dataframe['key'] is pandas series.


Solution

  • Not elegant, but I can use try-except

    grouped=dfKeys.groupby('key')
    for key, group in grouped:
        if ((group.someCol=='someVal').any() or ~(group.someCol.isin(someArray).any())):
            try: 
                foundIndex=dataframe[dataframe['key']==key].index.values.astype(int)[0] 
                dataframe.loc[foundIndex,'myCol']='myVal'
            except IndexError: print('key not found, continue')