Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

Python function causes changes to input pandas dataframe

$
0
0

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 pddata = {"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.


Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>