pari kannappan
pari kannappan

Reputation: 31

Pandas column with amount range

We have a column like below

name  salary-range
 A     $53K-$99K
 B     $41K-$78K
 c     $97K-$129K
 D     $29K-$38K

we need to find the name with highest salary

dtype of salary-range is object , is there any easy way to convert the column to int64 and check for the with highest salary?

Upvotes: 1

Views: 265

Answers (2)

jezrael
jezrael

Reputation: 863166

Use Series.str.extractall for get numbers, convert to integers:

s = (df.set_index('name')['salary-range']
       .str.extractall('(\d+)')[0]
       .astype(int)
       .reset_index(level=1, drop=True))
print (s)
name
A     53
A     99
B     41
B     78
c     97
c    129
D     29
D     38
Name: 0, dtype: int32

Last get names by maximal value by Series.idxmax:

a = s.idxmax()
print (a)
c

Upvotes: 1

Mayank Porwal
Mayank Porwal

Reputation: 34086

You can do this:

In [972]: df.sort_values('salary-range').tail(1)['name']
Out[972]: 
2    c
Name: name, dtype: object

Upvotes: 1

Related Questions