-1

I have 2 columns in the same data frame as shown:

Col1    Col2
YES     NO
YES     NO
YES     YES
YES     NO
YES     YES
        YES
        YES
        NO
        NO

I need to create a conditional: if Col1 == "YES" and Col2 == "NO" then COL2 = "YES", Meaning: if column 1 is "YES" and Column 2 is "NO" then COLUMN 2 is "YES"

I was trying with:

if df_db['Marketo LSC_x'] == "YES" and df_db['Marketo LSC_y'] == 'NO':
    df_db['Marketo LSC_y'] = 'YES'

but is not working, is giving me this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Berny
  • 113
  • 11
  • Why do you even need to check `col2`? Just change `col2` to `YES` whenever `col1` is `YES`. If it was already `YES`, no harm done... `df.loc[df.col1 == "YES", "col2"] = "YES"` – Tomerikoo Feb 24 '21 at 09:47
  • Does this answer your question? [Change one value based on another value in pandas](https://stackoverflow.com/questions/19226488/change-one-value-based-on-another-value-in-pandas) – Tomerikoo Feb 24 '21 at 09:51

1 Answers1

1

FYI, Logical operators for boolean indexing in Pandas.


Try this:

criteria_1 = df_db['Marketo LSC_x'] == "YES"
criteria_2 = df_db['Marketo LSC_y'] == 'NO'

criteria = criteria_1 & criteria_2

df_db.loc[criteria, 'Marketo LSC_y'] = 'YES'
Jaybe Park
  • 172
  • 1
  • 5
  • I tried and it gave me this error: TypeError: Cannot perform 'rand_' with a dtyped [object] array and scalar of type [bool] – Berny Feb 24 '21 at 09:31
  • I changed my answer because of my mistake. Let me know if you fix this error. – Jaybe Park Feb 24 '21 at 09:44