0

I have df that is similar to the following:

value  is_1  is_2 is_3
5      0     1    0
7      0     0    1
4      1     0    0

(it is guaranteed, that the sum of values from columns is_1 ... is_n is equal to 1 calculating by each row)

I need to get the next result:

is_1  is_2  is_3
0     5     0
0     0     7
4     0     0

(I should find the column is_k that is more than 0, and fill it with value from "value" column)

What is the best way to achieve it?

1 Answer 1

2

I'd do it this way:

In [16]: df = df.mul(df.pop('value').values, axis=0)

In [17]: df
Out[17]:
   is_1  is_2  is_3
0     0     5     0
1     0     0     7
2     4     0     0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.