I have a dictionary with several dataframes that looks like this:
dataframes = {'Df_20100101': DataFrame, 'Df_20100102': DataFrame, 'Df_20100103': DataFrame}
The keyname for each dataframe is composed by Df_ followed by the date 2010 [year], 01[month] and 01[day].
For each dataframe I want to add a new variable/column with the date [of course in the date format] that corresponds to its key.
I am kind of new learning to use dictionaries, so I would be really thankful if you can help me.
I tried with the following code, but it is pretty basic for what I want.
for key, val in dataframes.items():
val['Key']==k
Thanks in advance!
You have the right start.
for key, val in dataframes.items():
*_, date = key.split("_")
val["Date"] = pd.date_range(start=date, end=date, periods=len(val))
If you want to change the date format, you can do like this.
for key, val in dataframes.items():
*_, date = key.split("_")
val["Date"] = pd.date_range(start=date, end=date, periods=len(val)).strftime('%m/%d/%Y') #<==== here
And the documentation about date format.
The aford mentioned awnser is overcomplicating as when you passs a single value in a new column assignement, pandas will automaticly fill the entire column.
See @jezrael awser.
For changing the date format with @jezrael awser, just add format='%m/%d/%Y'
.
for key, val in dataframes.items():
dataframes[key]['Key'] = pd.to_datetime(key.split('_')[1], format='%m/%d/%Y')