0

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()
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Matthew
  • 55
  • 7
  • ``df['newval'] = 1`` isn't changing a variable, it changes an object. Objects don't have scope, they have identity. – MisterMiyagi Feb 21 '22 at 10:22
  • You are accessing object as reference, means mutable type, this is the reason why you are obtaining unexpected result. – baskettaz Feb 21 '22 at 10:28

0 Answers0