Is it possible to update the value of a global python variable from within a pandas lambda function?
I'm trying to
- use the value of this global variable in my lambda function
- change its value within the lambda function
- use the updated value in lambda function that's called on the subsequent rows
I've tried to declare the value of my global variable outside of the lambda function, but it's not accessible from within the lambda function itself. I also tried to pass it using
def do_something(x, global_var):
print(global_var)
if global_var > 0:
global_var = -1
return x/2
elif global_var <= 0:
global_var = 1
return 2*x
global_var = 5
df.apply(lambda x, global_var = global_var: do_something(x, global_var)
but updating global_variable from within the function doesn't update it globally. How, then, can I update the value of this variable from inside the lambda function?