pythonpandasdataframepandas-apply

How to iterate over an array using a lambda function with pandas apply


I have the following dataset:

     0    1     2   
0   2.0  2.0    4   
0   1.0  1.0    2   
0   1.0  1.0    3   
3   1.0  1.0    5   
4   1.0  1.0    2   
5   1.0  NaN    1   
6   NaN  1.0    1   

and what I want to do is insert a new column that iterates over each row, and if there is a NaN then give it a 0, if not then copy the value from column '2' to get this:

     0    1     2   3
0   2.0  2.0    4   4
0   1.0  1.0    2   2
0   1.0  1.0    3   3
3   1.0  1.0    5   5
4   1.0  1.0    2   2
5   1.0  NaN    1   0
6   NaN  1.0    1   0

The following code is what I have so far, which works fine but does not iterate over the values of column '2'.

df.isna().sum(axis=1).apply(lambda x: df[2].iloc[x] if x==0 else 0)

if I use df.iloc[x] I get

0    4
1    4
2    4
3    4
4    4
5    0
6    0

How can I iterate over the column '2'?


Solution

  • Try the below code with np.where with isna and any:

    >>> df['3'] = np.where(df[['0', '1']].isna().any(1), 0, df['2'])
    >>> df
         0    1  2  3
    0  2.0  2.0  4  4
    0  1.0  1.0  2  2
    0  1.0  1.0  3  3
    3  1.0  1.0  5  5
    4  1.0  1.0  2  2
    5  1.0  NaN  1  0
    6  NaN  1.0  1  0
    >>>