'Multiply values of nested dictionaries based on matching key

I would like to multiply the values inside nested dictionaries based their matching keys ('P1S1', 'P2S1', etc.) inside a loop. The problem is that the first element to multiply is a subpart of the hole value (i.e. a specific column of the dataframe contained by the value), and that the second value is a single int. As opposed as other examples I found on stackoverflow, I don't want to store the results in a new dictionary, but rather in a new column inside the original datframe.

dict_all_raw = { 'dict_20' : {"P1S1": df1, "P2S1": df2, "P3S1": df3, "P4S1": df4, 
               "P5S1": df5, "P6S1": df6, "P7S1": df7, "P8S1": df8,
               "P9S1": df9, "P10S1": df10},
               
               'dict_40' : {"P1S1": df11, "P2S1": df12, "P3S1": df13, "P4S1": df14, 
               "P5S1": df15, "P6S1": df16, "P7S1": df17, "P8S1": df18,
               "P9S1": df19, "P10S1": df20}}


dict_loads = { 'dict_20' : {"P1S1": 43, "P2S1": 34, "P3S1": 35, "P4S1": 33, "P5S1": 34, "P6S1": 34, 
               "P7S1": 34, "P8S1": 33, "P9S1": 43, "P10S1": 38, "P1S2": 43, "P2S2": 34, "P3S2": 35, "P4S2": 33, 
               "P5S2": 34, "P6S2": 34, "P7S2": 34, "P8S2": 33, "P9S2": 43, "P10S2": 38},
               
               'dict_40' : {"P1S1": 86, "P2S1": 68, "P3S1": 70, "P4S1": 65, "P5S1": 68, "P6S1": 68, 
               "P7S1": 68, "P8S1": 67, "P9S1": 86, "P10S1": 76, "P1S2": 86, "P2S2": 68, "P3S2": 70, "P4S2": 65, 
               "P5S2": 68, "P6S2": 68, "P7S2": 68, "P8S2": 67, "P9S2": 86, "P10S2": 76}}

For a single key, value pair, the operation I want to make would look like this :

dict_all_raw['dict_20']['P1S1']['new_col'] = dict_all_raw['dict_20']['P1S1']['acc'] * dict_loads['dict_20']['P1S1']

The dataframes inside 'dict_all_raw' are structured as follow:

    time  velocity       acc    
0   0.00  0.064360  0.000000
1   0.02  0.065363  0.050167
2   0.04  0.079842  0.723951
3   0.06  0.107262  1.370990
4   0.08  0.130649  1.169334
..   ...       ...       ...
64  1.28  0.402836 -0.829576
65  1.30  0.390045 -0.639557 
66  1.32  0.403470  0.671253
67  1.34  0.441059  1.879418
68  1.36  0.475240  1.709055

[69 rows x 3 columns]

Any help would be greatly appreciated !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source