Here in the below example, dataframe df
is passed from method_1
to method_2
. Additional column is added to original dataframe df
in method_2()
, but method_2()
does not return the changed dataframe df
. But changed dataframe is still reflecting in method_1()
when we print after the call. Please, can someone help me understand the behaviour?
import pandas as pd
class myclass:
def method_1(self):
data = {'Name':['Tom', 'nick', 'krish', 'jack'],
'Age':[20, 21, 19, 18]}
# Create DataFrame
df = pd.DataFrame(data)
print("BEFORE=",df)
self.method_2(df)
print("AFTER=",df) ## Here this captures changed dataframe although we dont return
def method_2(self, df):
df['newval'] = 1 ## Here we are not explicitly returning
a = myclass()
a.method_1()