Search code examples
pythonfunctionpass-by-referencepass-by-value

(Python)Localizing variable


My goal is to alter dataframe df using my_function then assign the result to dataframe df. But when I use function the dataframe df which is in the outside of the function is changed. How could I modify function not to affect the df varable which is outside of funtion?

import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30]}, index=['2021-11-24', '2021-11-25', '2021-11-26'])


def my_function(df_temp):
    df_temp['A'][0] = 100  # How could I modify not to affect df varable which is outside of funtion
    return df_temp         

   something = my_function(df)
   print(df)   # df is already altered although I didn't assign

# df = my_function(df)
# print(df)

Solution

  • Try these solutions

    1. Using pandas.apply function
    import pandas as pd
    df = pd.DataFrame({'A': [10, 20, 30]}, index=['2021-11-24', '2021-11-25', '2021-11-26'])
    
    
    def my_function(row):
        row[0] = 100       
        return row
    
    
    something = df.apply(my_function)
    print(something)
    
    A
    2021-11-24  100
    2021-11-25  20
    2021-11-26  30
    
    print(df)
    
    
    A
    2021-11-24  10
    2021-11-25  20
    2021-11-26  30
    



    2. Using pandas.copy function

    import pandas as pd
    df = pd.DataFrame({'A': [10, 20, 30]}, index=['2021-11-24', '2021-11-25', '2021-11-26'])
    
    def my_function(df):
        temp_df = df.copy()
        temp_df['A'][0] = 100
        return temp_df
    
    
    something = my_function(df)
    print(something)
    
    A
    2021-11-24  100
    2021-11-25  20
    2021-11-26  30
    
    print(df)
    
    
    A
    2021-11-24  10
    2021-11-25  20
    2021-11-26  30