I have a Python function that modifies a pandas dataframe in various ways. This includes dropping a column. Counter to my intuition, this drop operation is not limited to the environment (if that’s the right word) of the function, but passes out to the main coding environment, dropping the column in the input dataframe.
import pandas as pd
data = {
"name": ["Sally", "Mary", "John"],
"age": [50, 40, 30],
"qualified": [True, False, False]
}
df1 = pd.DataFrame(data)
def dropCol(df):
df.drop(columns='age',inplace=True)
dropCol(df1) #show that the "age" col is now gone from the input df
Is there a way to cordon off the function so that it cannot affect objects outside the function environment? I’d prefer my inputs to be unaffected and only have the function affect the object space via the return
operator.