I dont understand what is the best practice here:
I want to modify dataframe data
in my function. data
is defined globally. However, if I specify the global
option in the function, I necessarily get an error because data =
defines a local variable.
data = pd.DataFrame({'A' : [1, 2, 3, 4],
'B' : [1, 2, 3, 4]})
def test(data):
global data
data = data + 1
return data
test(data)
SyntaxError: name 'data' is local and global
Does that mean I cannot use the global
argument when working with dataframes?
def test2(data):
data = data + 1
return data
does not work either. That is the original data
is not modified.
What am I missing here?