Assuming I'm dealing with this dataframe:
| Total | Retired | Cancelled | Remaining |
|---|---|---|---|
| 100 | 10 | 20 | 70 |
| 150 | 160 | 30 | -40 |
| ... | ... | ... | ... |
where
df['Remaining'] = df['Total'] - df['Retired'] - df['Cancelled']
My desired output should be like this from previous dataframe (to manually revise the negative ones in 'Remaining' to zeros):
| Total | Retired | Cancelled | Remaining |
|---|---|---|---|
| 100 | 10 | 20 | 70 |
| 190 | 160 | 30 | 0 |
| ... | ... | ... | ... |
I tried these lines, but it returns error. What did I do wrong?
df['Total'] = df.apply(lambda x: (x['Retired'] + x['Cancelled']) if x['Remaining'] < 0 else x['Total'])
(and do the previous calculation again for final output)
df['Remaining'] = df['Total'] - df['Retired'] - df['Cancelled']