Search code examples
pythonmedianmodetry-except

Use 2 exceptions in a python function


I want to write a function to calculate the mode of a column in a grouped by dataframe. If the values of a group has not a mode, the first exception: calculate the median and if the values of the group are all null values, the second exception:do nothing. The sample dataframe is like below:

dataframe = pd.DataFrame({'b':['b1','b1','b1','b1','b1','b1','b2','b2','b2','b2','b2','b2','b3','b3','b3'],'d':[0.1,None,0.12,None,None,0.13,1,2,1,1,None,None,None,None,None]})

The function is as follow:

def fill_mode(group):
    try:
        group['mode'] = mode(group['d'])
    except:
        not_nulls = group[~group['d'].isnull()]
        group['mode'] = median(not_nulls['d'])
    except:
        pass
    return group 

And the apply function is as bellow:

dataframe = dataframe.groupby('b').apply(fill_mode)

Which raises this error :

SyntaxError: default 'except:' must be last

The final output should be like this:

enter image description here


Solution

  • You need a second try/except block.

    def fill_mode(group):
        try:
            group['mode'] = mode(group['d'])
        except:
            try:
                not_nulls = group[~group['d'].isnull()]
                group['mode'] = median(not_nulls['d'])
            except:
                pass
        return group