1

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'?

1 Answer 1

2

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
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

To be more precise, you can replace df.isna().any(1) by df[['0', '1']].isna().any(1)
@Corralien That is true, edited my answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.