Skip to content
Advertisement

If a column has ‘x’ value, find max value from other columns in row, and change x to the value of max column name

I have a dataframe similar to the following:

JavaScript

If the column topic value is -1, then I want to look in the same row of columns 0 to 2, and change the value in topic, to the header of the max value.

enter image description here

As an example, in the first row in the table above, the column topic has a value of -1, so I want to change that value to whatever is the header name of the max value in the row. So that would be column 2, which has a value of 0.4.

So now the table looks like this, where the topic value of ref 0001 has changed from -1 to 2

enter image description here

The other point is, that if like ref 0006, there is no value > 0 in the other columns despite having a topic value of -1, then it should be left alone.

I hope this makes sense. Struggling hard to get this done.

Thank you!

Advertisement

Answer

Setting up example:

JavaScript

Making a boolean mask to set the rows:

JavaScript

using idxmax gets the names of the columns at the maximum values

JavaScript

Now you can use df.where and the boolean condition to set the topic values.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement