I've been experimenting for a while with pd.Series and pd.DataFrame and faced some strange problem. Let's say I have the following pd.DataFrame:
df = pd.DataFrame({'col':[[1,2,3]]})
Notice, that this dataframe includes column containing list. I want to modify this dataframe's copy and return its modified version so that the initial one will remain unchanged. For the sake of simplicity, let's say I want to add integer '4' in its cell.
I've tried the following code:
def modify(df):
dfc = df.copy(deep=True)
dfc['col'].iloc[0].append(4)
return dfc
modify(df)
print(df)
The problem is that, besides the new copy dfc
, the initial DataFrame df
is also modified. Why? What should I do to prevent initial dataframes from modifying? My pandas version is 0.25.0