0

Google provided me with the code:

for i in df.index:
    df.at[i, 'job'] = 0 if df.at[i, 'job'] == 'unemployed' else 1

But python says: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I'm not sure how to fix it.

  • 1
    As Jan has pointed out, you don't need to iterate over the rows. I would use a Boolean series instead: `df['job'] = (df.job != 'unemployed').astype(int)` – Arne Dec 16 '22 at 16:26

1 Answers1

0

You don't need to iterate over the rows. You can use apply():

df['job'] = df['job'].apply(lambda x: 0 if x == 'unemployed' else 1)

Or you can use numpy's where:

df['job'] = np.where(df['job']=='unemployed', 1, 0)

Or as Arne pointed out in his comment, you can convert the boolean result of the comparison to int:

df['job'] = (df.job != 'unemployed').astype(int)
DataJanitor
  • 1,276
  • 1
  • 8
  • 19