I have a dataframe which looks like this.
s_id h_id h_val h_others
1 600 5 {700,500}
1 700 12 {600,500,400}
1 500 6 {600,700}
2 ... ... ...
What I want to do is, when grouped by s_id
, iterate over h_others
, see if each id in the dictionaries is found in h_id
for this particular s_id
. If it is found, I want to map its value which can be found in h_val
, add them up, and create a new column with the sum of the mapped values of h_others
. If it is not found, the id can just be mapped to 0, so that it doesn't impact the sum.
Expected output:
s_id h_id h_val h_others sum_h_others
1 600 5 {700,500} 18
1 700 12 {600,500,400} 11
1 500 6 {600,700} 17
2 ... ... ...