If the dictionary and additional dataframe are dictionary
and df
, you can concat()
the dictionary
dataframes with the corresponding df
row:
for key in dictionary.keys():
dfs = [dictionary[key], df.loc[df.label == key].reset_index(drop=True)]
dictionary[key] = pd.concat(dfs, axis=1)
Full example
Given dictionary
with dataframes for sample_1_200celsius
and sample_1_300celsius
:
df200 = pd.DataFrame({'displacement': [1,2,3], 'values': [2,4,6]})
df300 = pd.DataFrame({'displacement': [30,20,10], 'values': [60,40,20]})
dictionary = {'sample_1_200celsius': df200, 'sample_1_300celsius': df300}
And additional dataframe df
:
df = pd.DataFrame({'label': ['sample_1_200celsius','sample_1_300celsius'], 'width': [11,12], 'thickness': [222,223]})
# label width thickness
# 0 sample_1_200celsius 11 222
# 1 sample_1_300celsius 12 223
You can concat()
each dictionary
dataframe with the corresponding row from df
using loc
indexing:
for key in dictionary.keys():
dfs = [dictionary[key], df.loc[df.label == key].reset_index(drop=True)]
dictionary[key] = pd.concat(dfs, axis=1)
So then the dictionary
values will be updated as desired:
dictionary['sample_1_200celsius']
# displacement values label width thickness
# 0 1 2 sample_1_200celsius 11.0 222.0
# 1 2 4 NaN NaN NaN
# 2 3 6 NaN NaN NaN
dictionary['sample_1_300celsius']
# displacement values label width thickness
# 0 30 60 sample_1_300celsius 12.0 223.0
# 1 20 40 NaN NaN NaN
# 2 10 20 NaN NaN NaN