0

I have 12 columns of ingredients and 12 respective columns of measurements of those ingredients. For some rows the ingredients are specified but the measurements are not i.e. they are NA. I want to set up a condition such that if a certain ingredient entry is not NA and the corresponding entry is NA, set the corresponding measurement entry to 1.

cols1 = ["strIngredient1","strIngredient2","strIngredient3","strIngredient4","strIngredient5","strIngredient6",
        "strIngredient7","strIngredient8","strIngredient9","strIngredient10","strIngredient11","strIngredient12"]

cols2 = ["strMeasure1","strMeasure2","strMeasure3","strMeasure4","strMeasure5","strMeasure6","strMeasure7",
         "strMeasure8","strMeasure9","strMeasure10","strMeasure11","strMeasure12"]

1 Answer 1

1

if a certain ingredient entry is not NA and the corresponding entry is NA, set the corresponding entry to 1

If I understand correctly, you can use mask to replace values where the condition is True.

for col1, col2 in zip(cols1, cols2):
    df[col2] = df[col2].mask(df[col1].notna()&df[col2].isna(), 1)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! Another question. I tried using the condition: for col1, col2 in zip(cols1, cols2): if df[col1].notna()&df[col2].isna(): df[col2].isna().sum. but I got a value error saying ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
@kapsha You are using Series in if condition, See this for more detail.

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.