So I have a dataframe:
import pandas as pd
df = pd.DataFrame({'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'score': [1, 3, 4, 5, 2]})
And I want to create a new column based on the conditions in the 'score' column.
I tried it out like this
df['happiness'] = df['score']
def are_you_ok(df):
if df['happiness'] >= 4:
return 'happy',
elif df['happiness'] <= 2:
return 'sad',
else:
return 'ok'
df['happines'] = df['happiness'].apply(are_you_ok)
df
When I try to run that though, all I get is:
TypeError: 'int' object is not subscriptable
Can I not use this kind of function with an integer?