I have the following table:
A | B | C |
---|---|---|
0 | 0 | 0 |
2022-02-15 00:00:00 | 0 | 2022-03-46 00:00:00 |
2022-04-23 00:00:00 | 2021-01-10 00:00:00 | 0 |
I would like to get a resulting column that returns the column name of the latest date per row and ignore all empty rows (this is part of a bigger dataset so I can't dropna). So as follows:
A | B | C | Result |
---|---|---|---|
0 | 0 | 0 | 0 |
2022-02-15 00:00:00 | 0 | 2022-03-46 00:00:00 | C |
2022-04-23 00:00:00 | 2021-01-10 00:00:00 | 0 | A |
I converted the columns to datetime and tried using:
df['Result'] = df.idxmax(axis=1)
But this only resulted in the following error
TypeError: reduction operation 'argmax' not allowed for this dtype
CodePudding user response:
You need to have datetime type.
If you don't want to alter the dataframe, you can do it on the fly:
df['Result'] = df.apply(pd_to_datetime).idxmax(axis=1).mask(df.eq(0).all(1), 0)