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"]
kapsha
  • 35
  • 4

1 Answers1

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)
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • 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 Apr 30 '22 at 18:27
  • @kapsha You are using Series in `if` condition, See [this](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o/71956180#71956180) for more detail. – Ynjxsjmh Apr 30 '22 at 18:34