简体   繁体   中英

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.

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM