How to iterate through data frame and edit values?

Question:

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.

Asked By: Walter125

||

Answers:

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)
Answered By: Jan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.