Python: create a new column from existing columns

前端 未结 6 2033
一生所求
一生所求 2021-02-20 02:48

I am trying to create a new column based on both columns. Say I want to create a new column z, and it should be the value of y when it is not missing and be the value of x when

6条回答
  •  被撕碎了的回忆
    2021-02-20 03:16

    The update method does almost exactly this. The only caveat is that update will do so in place so you must first create a copy:

    df['z'] = df.x.copy()
    df.z.update(df.y)
    

    In the above example you start with x and replace each value with the corresponding value from y, as long as the new value is not NaN.

提交回复
热议问题