I have a dictionary of DataFrames with the key referring to the year of the data. I would like to iterate through the dict and make modifications to the DataFrames. I make modifications to both the column names and the contents of the dfs.
for year, df in df_data.items():
cols = df .columns
new_cols = [re.sub(r'\s\d{4}\-\d{2}', '', c) for c in cols]
df.columns = new_cols
for year, df in df_data.items():
df['Date'] = pd.to_datetime(df['Date'], infer_datetime_format=True)
df = df.drop_duplicates(subset='Id', keep='first')
Can someone explain to me the behavior of doing this? Particularly, how the dfs are stored in memory and why the rename of columns works but the modification to the contents do not. Also, is there a best way to do this either by copying the df and then replacing it in the dict index or by constantly making the changes to the df_data[year] reference?