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.
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)
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.
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)