-1

I have three fields, client_id, flag_1 (0 or 1), and flag_2 (0 or 1). For every client, I need to calculate flag_3 which should equal 1 if both flag_1 == 1 and flag_2 ==1, otherwise 0.

I have tried:

df['flag_3'] = [1 if x==1 and y==1 else 0 for x in df['flag_1'] and y in df['flag_2']]

df['flag_3'] = np.where(df['flag_1'] == 1 and df['flag_2'] == 1, 1, 0)

...getting errors like "The truth value of a Series is ambiguous."

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
gmf
  • 1
  • You need to use bitwise operator (&) instead of logical (and) – Celius Stingher Jun 02 '20 at 22:46
  • _...getting errors like "The truth value of a Series is ambiguous."_ Please provide the entire error output, as well as a [mcve]. – AMC Jun 03 '20 at 00:23
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – AMC Jun 03 '20 at 00:23

1 Answers1

1

You are using boolean indexing to select rows and the index should be combined using binary "&"

df['flag_3'] = np.where((df['flag_1'] == 1) & (df['flag_2'] == 1), 1, 0)
adrtam
  • 6,991
  • 2
  • 12
  • 27