Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: create a new column from existing columns

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 y is indeed missing. So in this case, I expect z to be [1, 8, 10, 8].

   x   y
0  1 NaN
1  2   8
2  4  10
3  8 NaN
like image 925
Kexin Xu Avatar asked May 15 '15 17:05

Kexin Xu


2 Answers

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)
like image 140
Haleemur Ali Avatar answered Sep 28 '22 22:09

Haleemur Ali


The new column 'z' get its values from column 'y' using df['z'] = df['y']. This brings over the missing values so fill them in using fillna using column 'x'. Chain these two actions:

>>> df['z'] = df['y'].fillna(df['x'])
>>> df
   x   y   z
0  1 NaN   1
1  2   8   8
2  4  10  10
3  8 NaN   8
like image 38
Vidhya G Avatar answered Sep 28 '22 22:09

Vidhya G