Rfanatic
Rfanatic

Reputation: 2282

How to replace specific elements

I am using time use data and I would like to replace specific elements at a specific time step.

I have a 2 data frames with the following structure:

df1

a   b   c  d
11  15  34 21
34  4   5  11
7   8   9  11
8   9   1  11

df2

a  b  c  d
0  1  0  0
0  1  1  1
0  0  0  1
0  1  1  0

I would like to create df3 data frame by the following rule:

the 0's in df2 to be replaced with 0 in df2 the 1's in df2 to replaced with the value in df2

The new df3 structure

a  b   c   d 
0  15  0   0
0  4   0   0
7  0   9   0
0  0   1   0

Any help please?

Upvotes: 1

Views: 34

Answers (1)

akrun
akrun

Reputation: 887291

An option would be to multiply the binary dataset ('df2') with the first one so that any element that corresponds to position where the value is 0 becomes 0 and those corresponds to 1 becomes the value itself

df1 * df2

Upvotes: 2

Related Questions