lwu29
lwu29

Reputation: 111

Comparing rows in Pandas

I have a pandas data frame that looks something like this:

df = pd.DataFrame( [ ['A','one',7], ['A','two',8], ['B','one',9], ['B','two',6]], columns=['ID', 'Type', 'Price'])

    ID  Type    Price
0   A   one     7
1   A   two     8
2   B   one     9
3   B   two     6

I want to add a column that's the result of comparing 'Price' among each ID. So the result looks like this:

    ID  Type    Price   Level
0   A   one        7    low
1   A   two        8    high
2   B   one        9    high
3   B   two        6    low

I'm looking for an efficient way to do this. Thanks!

Upvotes: 2

Views: 38

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

Let's try duplicated:

df['Level'] = (df.sort_values('Price').duplicated(['ID'])
                 .map({True:'high', False:'low'})
              )

Output:

  ID Type  Price Level
0  A  one      7   low
1  A  two      8  high
2  B  one      9  high
3  B  two      6   low

Upvotes: 2

BENY
BENY

Reputation: 323226

We can try groupby + rank , then map

df['Level']=df.groupby('ID').Price.rank().map({1:'low',2:'high'})
df
Out[221]: 
  ID Type  Price Level
0  A  one      7   low
1  A  two      8  high
2  B  one      9  high
3  B  two      6   low

Upvotes: 3

Related Questions