I have the below dataframe called df:
Id | Stage1 | Stage2 | Stage3 |
---|---|---|---|
1 | 2022-02-01 | 2020-04-03 | 2022-06-07 |
--- | ------------ | ------------ | ----------- |
2 | 2023-06-07 | 2020-03-01 | 2020-09-03 |
--- | ------------ | ------------ | ----------- |
3 | 2023-02-04 | 2023-06-07 | 2022-06-07 |
--- | ------------ | ------------ | ----------- |
4 | 2022-05-08 | 2023-09-01 | 2023-09-01 |
I need to calculate the max date for each ID and its respective Stage. So for Order 1,2,3 the Stages I need are Stage 3, Stage 1 and Stage 2 respectively. I started this process by calculating the max date in each row first with the below code:
df2 = df[['Stage1', 'Stage2', 'Stage3', 'Stage4', 'Stage5']]
lis = list(df2.max(axis=1))
lis variable has the max dates stored for each row. Now, with each max date, I need to get the Stage Name of that row.
The below code calculates the max Stage for the whole df and not row.
new_lis = []
for i in lis:
new_lis.append(df.columns[df.isin([i]).any()])
How do I fix this? Output I need is "Stage 3", "Stage 1" and "Stage 2" for Order 1,2,3 respectively.