I'm new to pandas and working with indices, especially MultiIndex. I have a DataFrame as such:
df = pd.DataFrame({
'ID':[1,2,1,2],
'Measurement': ['ScanA', 'ScanA', 'ScanB', 'ScanB'],
'Result':[0.1,0.2,0.5,0.7],
'ResultType':['A','B','C','B']})
piv = df.pivot(index = 'ID', columns = 'Measurement', values = ['Result', 'ResultType'])
This creates two indices Result and Type, but I'd like to modify the values in the Result index based on the Type value.
For example, if Type == 'C' then I want the corresponding Result to be -1.
Another example would be if Type in ('A', 'B') then I want to set Result to be 0 if < 0.5 else 1
How can I programatically do this without looping through each row/column?
Output for piv.Result would look like:
Measurement ScanA ScanB
ID
1 0.0 -1
2 0.0 1