I am processing a pandas dataframe df
. Depending on certain conditions within df
, I want to pass df
(1) either to the user defined function filter()
or (2) to calculate_Result()
.
The code for the condition is:
if df.Col1.str.contains("yes").sum() > 0:
df = filter_df
filter() # Loop
else:
df = calculate_df
calculate_result() # End
However, I am yielding a UnboundLocalError: local variable referenced before assignment.
I keep getting this error also when I put the df = calculate_df
& df = filter_df
assignments in the function definitions itself:
def filter():
df = filter_df
...
and
def calculate_results():
df = calculate_df()
...
no matter where I put the assignments, I am yielding UnboundLocalError: local variable referenced before assignment.
How to correctly pass a local variable to a user defined function in pandas?