Reputation: 41
I am trying to create a column that categorizes a number rating as Positive, Neutral, Negative based on a value in an adjacent column in a dataframe. Any ideas? Thanks!
#create Target Column "Positive, Neutral, Negative"
df['Target'] = df.StarsInt.map((5:'Positive',4:'Positive',3:'Neutral',2:'Negative',1:'Negative'))
Upvotes: 1
Views: 823
Reputation: 153460
Use pd.cut
:
pd.cut(s, bins=[0,2,3,6], labels=['Negative', 'Nuetral', 'Positive'])
Output:
1 Negative
2 Negative
3 Nuetral
4 Positive
5 Positive
6 Positive
Upvotes: 2
Reputation: 10960
Use np.select
conditions = [
df.StarsInt >= 4,
df.StarsInt == 3,
df.StarsInt <= 2,
]
values = ['Positive', 'Neutral', 'Negative']
df['Target'] = np.select(conditions, values)
Upvotes: 2