Python: create a new column from existing columns

前端 未结 6 2068
一生所求
一生所求 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:28

    You can use apply with option axis=1. Then your solution is pretty concise.

    df[z] = df.apply(lambda row: row.y if pd.notnull(row.y) else row.x, axis=1)
    

提交回复
热议问题