2

For pairs of columns, i want to replace the values of the second columns with nan if the values in the first is nan. I have tried without success

>import pandas as pd
> 
> df=pd.DataFrame({'a': ['r', np.nan, np.nan, 's'], 'b':[0.5, 0.5, 0.2,
> 0.02],  'c':['n','r', np.nan, 's' ], 'd':[1,0.5,0.2,0.05]})
> 
>listA=['a','c']

>listB=['b','d']

>for color, ratio in zip(listA, listB):

>>df.loc[df[color].isnull(), ratio] == np.nan

df remain unchanged

other test using def (failed)

>def Test(df):
>> if df[color]== np.nan:
>> >> return df[ratio]== np.nan
>> else:
>> >>return


    
>for color, ratio in zip(listA, listB):
>>>>df[ratio]=df.apply(Test, axis=1)

Thanks

0

1 Answer 1

1

It seems you have typo, change == to =:

for color, ratio in zip(listA, listB):
    df.loc[df[color].isnull(), ratio] = np.nan
print (df)
     a     b    c     d
0    r  0.50    n  1.00
1  NaN   NaN    r  0.50
2  NaN   NaN  NaN   NaN
3    s  0.02    s  0.05

Another solution with mask for replace True values of mask to NaN by default:

for color, ratio in zip(listA, listB):
    df[ratio] = df[ratio].mask(df[color].isnull())
print (df)
     a     b    c     d
0    r  0.50    n  1.00
1  NaN   NaN    r  0.50
2  NaN   NaN  NaN   NaN
3    s  0.02    s  0.05
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks a lot, this completely answers my question

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.