Supertech
Supertech

Reputation: 770

Replace column value with another column value if condition met

I have a df in which I first split col1 at "-". This results "None" values in col3 if delimiter does not exist in the col1.

Now, if col3 is "None", I want to replace it with col2 values (inplace replacement)

#df
 col1 col2 col3
45-65 45 65   
   56 56 None

#desired df
 col1 col2 col3
45-65 45 65   
   55 55 55

How can I do this in pandas.

Upvotes: 0

Views: 91

Answers (1)

BeRT2me
BeRT2me

Reputation: 13242

df = df.ffill(axis=1)
print(df)

Output:

    col1 col2 col3
0  45-65   45   65
1     56   56   56

Upvotes: 1

Related Questions