I face a problem of modification of a dataframe inside a function that I have never observed previously. Is there a method to deal with this so that the initial dataframe is not modified.
def test(df):
df['tt'] = np.nan
return df
dff = pd.DataFrame(data=[])
Now, when I print dff
, the output is
Empty DataFrame
Columns: []
Index: []
If I pass dff
to test()
defined above, dff
is modified. In other words,
df = test(dff)
print(dff)
now prints
Empty DataFrame
Columns: [tt]
Index: []
How do I make sure dff
is not modified after being passed to test()
?
None
. – Joycejoycelin.copy()
to take an explicit deep copy – Gurleydf
at the end of the function, I don't think you can avoid doing a.copy()
– Aurilia