dlanced
dlanced

Reputation: 1110

Update the value of a variable from within a Python function as it iterates through a dataframe

I'm trying to create a Python function that will iterate through the rows of a Pandas dataframe and execute some simple arithmetic using a variable. But I'd also like to populate the original variable with the product that the function returns.

The goal is to convert the dataframe (which currently contains quarterly annual average percentages) to an approximate dollar figure based on a given starting point ($177.1 in this case). But I can only do that if each subsequent iteration is given an updated base dollar figure.

This version of the function works:

newnum = 177.1
def process_wages(x):
    if type(x) is str:
        return x
    elif x:
        return (x / 400) * newnum + newnum
    else:
        return
        
newdf.applymap(process_wages)

But I'd like to add a line that might look something like this:

    else:
        return
    newnum = x
newdf.applymap(process_wages)

...which, of course, doesn't work - returning this error:

UnboundLocalError: local variable 'newnum' referenced before assignment

Update: Here's the input dataframe:

    Date    Percent
0   2002Q1  3.5
1   2002Q2  3.6
2   2002Q3  3.1
3   2002Q4  2.6
4   2003Q1  2.9
... ... ...
70  2019Q3  3.0
71  2019Q4  3.0
72  2020Q1  3.3
73  2020Q2  2.9
74  2020Q3  2.7
75 rows × 2 columns

And here's the output I get from the working function (the problem is, of course, that each iteration is working from the same, original, value of newnum):

    Date    Percent
0   2002Q1  178.649625
1   2002Q2  178.693900
2   2002Q3  178.472525
3   2002Q4  178.251150
4   2003Q1  178.383975
... ... ...
70  2019Q3  178.428250
71  2019Q4  178.428250
72  2020Q1  178.561075
73  2020Q2  178.383975
74  2020Q3  178.295425
75 rows × 2 columns

Upvotes: 0

Views: 122

Answers (2)

dlanced
dlanced

Reputation: 1110

Using Maria's tip, I reworked my function to look like this:

newnum = 177.1
def process_wages(x):
    global newnum
    if type(x) is str:
        return x
    elif x:
        newnum = (x / 400) * newnum + newnum
        return newnum
    else:
        return
newstuff = newdf.applymap(process_wages)

...and now I'm able to pass newnum back to the global variable AND to my dataframe.

Upvotes: 1

Maria
Maria

Reputation: 397

global can be used to manipulate a variable outside of a function.

Upvotes: 1

Related Questions