Skip to content
Advertisement

Can I shift specific values in one data column to another column while keeping the other values unchanged?

Here is an example dataset that I have:

C1      C2
 1       1
NaN      1
 2       0
NaN      0
NaN      1
 1       1
 2       2
 2       2
NaN      1

I want to take all the values that have “1” in them in the Column “C2” and shift them to replace the adjacent values in column “C1”.

So the output should look like:

C1      C2
 1       1
 1       1
 2       0
 NaN     0
 1       1
 1       1
 2       2
 2       2
 1       1

Alternatively, I could create a new column with these values replaced. Main point is, that I need all the “1s” in C2 TO replace the NaN values in C1.

I can’t do find all NaN and replace with 1, because there are some NaN values that should stay in C1.

Is there a way to do this?

Thanks for the help in advance.

Advertisement

Answer

You could use the DF.mask() api to apply values from one column to another in rows where some condition is true (e.g. ==1).

value = 1
source_col = 1
target_col = 0

condition = df[source_col] == value

df[target_col] = df[target_col].mask(condition,
                                     df[source_col])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement