I have a DataFrame which looks like that:
Something1 Something2
date 2020-03-30 2020-03-31 2020-04-01 2020-03-30 2020-03-31 2020-04-01
index_1 index_2 index_3 index_4
A0 B0 C0 D0 10 NaN 11 'bla' 'bli' 'blo'
A1 B1 C1 D1 8 NaN NaN 'bla1' 'bli1 'blo1'
A2 B2 C2 D0 0 NaN 303 'bla2' 'bli2' 'blo2'
When the index_[1|2|3|4] are a MultiIndex. Something[1|2] are first level columns. and the dates are the a repetitive second level columns which are same across the Something[1|2] first level columns.
Now, I got a different table which contains values to I want to enrich with my current table. The new table looks like:
index_4 date val
D0 2020-03-30 8
D0 2020-03-31 9
...
D1 2020-03-30 17
D1 2020-03-31 33
I want to enrich the old table using the old one.
My Questions is: How do I fill the old table using the new table - when I only want to set the value in Something1
, so it would look like (after the filling of D0 in the date 2020-03-31
in Something1):
Something1 Something2
date 2020-03-30 2020-03-31 2020-04-01 2020-03-30 2020-03-31 2020-04-01
index_1 index_2 index_3 index_4
A0 B0 C0 D0 10 9 11 'bla' 'bli' 'blo'
A1 B1 C1 D1 8 NaN NaN 'bla1' 'bli1' 'blo1'
A2 B2 C2 D0 0 9 303 'bla2' 'bli2' 'blo2'
I've tried to set the value using set_value
, xs
, at
but I can't find the correct combo to get to the correct cell. (also looked in this post for some ideas but without successes )
I guess it suppuse to look like something like that
df.at[index_4='D0']['Something1']['2020-03-31'] = new_df['D0', '2020-03-31']['val']