Invalid syntax else

def IV_calc(data,var):
… if data[var].dtypes == “object”:
… dataf = data.groupby([var])[‘class’].agg([‘count’,‘sum’])
… dataf.columns = [“Total”,“bad”]
… dataf[“good”] = dataf[“Total”] - dataf[“bad”]
… dataf[“bad_per”] = dataf[“bad”]/dataf[“bad”].sum()
… dataf[“good_per”] = dataf[“good”]/dataf[“good”].sum()
… dataf[“I_V”] = (dataf[“good_per”] - dataf[“bad_per”]) * np.log(dataf[“good_per”]/dataf[“bad_per”])
… return dataf

else:

… data[‘bin_var’] = pd.qcut(data[var].rank(method=‘first’),10)
dataf = data.groupby([‘bin_var’])[‘class’].agg([‘count’,‘sum’])
… dataf.columns = [“Total”,“bad”]
… dataf[“good”] = dataf[“Total”] - dataf[“bad”]
… dataf[“bad_per”] = dataf[“bad”]/dataf[“bad”].sum()
… dataf[“good_per”] = dataf[“good”]/dataf[“good”].sum()
… dataf[“I_V”] = (dataf[“good_per”] - dataf[“bad_per”]) *np.log(dataf[“good_per”]/dataf[“bad_per”])
… return dataf

When I run the above code I am getting the below error :
else:
^
SyntaxError: invalid syntax

please help.

The indentation is wrong: your “else” clause is outside of the function,
and so has no “if” partner.

You have:

def IV_calc(data, var):
    if condition:
        block

else:
# Lined up with "def", which is wrong.
    block

Should be:

def IV_calc(data, var):
    if condition:
        block

    else:
    # Lined up with "if"
        block

@steven.daprano Thank you