I have a pandas
data frame as follows:
id group type action cost
101 A 1 10
101 A 1 repair 3
102 B 1 5
102 B 1 repair 7
102 B 1 grease 2
102 B 1 inflate 1
103 A 2 12
104 B 2 9
I need to reshape it from long to wide, but depending on the value of the action
column, as follows:
id group type action_std action_extra
101 A 1 10 3
102 B 1 5 10
103 A 2 12 0
104 B 2 9 0
In other words, for the rows with empty action
field the cost
value should be put under the action_std
column, while for the rows with non-empty action
field the cost
value should be summarized under the action_extra
column.
I've attempted with several combinations of groupby
/ agg
/ pivot
but I cannot find any fully working solution...