Reut
Reut

Reputation: 1592

Getting UnboundLocalError when definning global inside the function

I know this is very cmmon error but I can find my error. I have dataframe with perimeter data ,e.g:

>>>name    perimeter
0  al      24.5
1  dl      43.7
2  yl      1222.4
3  pl      213.2
4  kl      120
...

I want to check what is the median perimeter and then add new column according to the perimeter median value. I define the median before the function and then I define the median as global inside the function:

per_median=df['perimeter'].median()

def z(row):
    global per_median
    if row['perimeter'] > per_median:
        val = 'yes'
    elif row['perimeter'] < per_median:
        val = 'no'
    return val

df['perimeter_warning'] = df.apply(z,axis=1)

for some reason, even though I use global inside the function, I keep getting this error:

UnboundLocalError: local variable 'val' referenced before assignment

What do I do wrong? I was excpected that define it as global will solve the problem.

Upvotes: 2

Views: 30

Answers (2)

Aviv Yaniv
Aviv Yaniv

Reputation: 6298

Handling the case that row['perimeter'] equals per_median was missing, so variable val was not set.

Edited the code to handle that scenario:

import pandas as pd
import numpy as np


df = pd.DataFrame({'name':['al', 'dl', 'yl', 'pl', 'kl'],
'perimeter':[24.5, 43.7, 1222.4, 213.2, 120]})


per_median=df['perimeter'].median()

def z(row):
    global per_median
    if row['perimeter'] > per_median:
        val = 'yes'
    elif row['perimeter'] < per_median:
        val = 'no'
    else:
        val = 'equals'
    return val

df['perimeter_warning'] = df.apply(z,axis=1)

print(df)

Output:

  name  perimeter perimeter_warning
0   al       24.5                no
1   dl       43.7                no
2   yl     1222.4               yes
3   pl      213.2               yes
4   kl      120.0            equals

Upvotes: 0

Mahesh Anakali
Mahesh Anakali

Reputation: 344

Try adding one more scenario in the code where

if row['perimeter'] == per_median: Then set some value for val.

Or if you dont want to handle this, try to assign a default value for val before using it.

Upvotes: 1

Related Questions