lexalexa_1990
lexalexa_1990

Reputation: 13

Pandas, editing dataframe

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?

Upvotes: 0

Views: 86

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210822

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

Upvotes: 2

Related Questions