Eunsoo Ko
Eunsoo Ko

Reputation: 23

marking same across rows if one of rows satisfy condition

How can I mark as 'abuser' across rows of same ID if one of rows of that ID satisfy a condition?

For example, if I have the following table,

ID social score
1 0
1 2
1 3
2 3
2 1
2 2

Can I mark rows of ID 1 as all abnormal (because social score of one of rows is 0 ) and mark rows of ID 2 as all normal (because social score of none of rows of ID 2 is 0 )?

ID social score class
1 0 'abnormal'
1 2 'abnormal'
1 3 'abnormal'
2 3 'normal'
2 1 'normal'
2 2 'normal'

If I can, with what python phrase?

Upvotes: 2

Views: 49

Answers (2)

Shubham Sharma
Shubham Sharma

Reputation: 71689

First find the ID's having social score 0, then use np.where to assign class values:

i = df.loc[df['social score'].eq(0), 'ID']
df['class'] = np.where(df['ID'].isin(i), 'abnormal', 'normal')

   ID  social score     class
0   1             0  abnormal
1   1             2  abnormal
2   1             3  abnormal
3   2             3    normal
4   2             1    normal
5   2             2    normal

Upvotes: 3

Dejene T.
Dejene T.

Reputation: 989

Try this

df['class'] = df.ID.apply(lambda x: 'abnormal' if x==1 else 'normal')

Upvotes: 0

Related Questions