Kaelan McGurk
Kaelan McGurk

Reputation: 13

How to find the lowest number of a grouped dataframe in Python

I have a table and I want to find the lowest number associated with each 'leadId' in my table. Here is a snapshot of it below:

Index leadId repId hoursSinceContacted
1 261 1111 10391
2 261 2222 10247
3 261 3333 1149
4 261 4444 10247
5 262 5555 551
6 262 6666 982
6 262 3333 214

Is there a groupby statement I can use to get a table that looks like this?:

Index leadId repId hoursSinceContacted
3 261 3333 1149
6 262 3333 214

Any suggestion will be much appreciated.

Upvotes: 1

Views: 50

Answers (3)

try using as_index=False

df.groupby(['leadid'],as_index=False).agg({'hoursSinceContacted':['min','max']})


or

fp = df.pivot_table( columns='leadId', values='hoursSinceContacted', aggfunc={'min'})
print(fp)

output:

leadId   261  262
min     1149  214

Upvotes: 1

Kenan
Kenan

Reputation: 14094

Groupby and then min

>>> df.groupby('leadId').min()
        Index  repId  hoursSinceContacted
leadId                                   
261         1   1111                 1149
262         5   3333                  214

Updated per @ALoliz comment, if you want the rows corresponding to the min(hoursSinceContacted)

df.loc[df.groupby('leadId')['hoursSinceContacted'].idxmin()]

   Index  leadId  repId  hoursSinceContacted
2      3     261   3333                 1149
6      6     262   3333                  214

Upvotes: 1

acrobat
acrobat

Reputation: 917

You can do:

df.groupby('leadid').agg({'hoursSinceContacted' : 'min'}).reset_index()

Upvotes: 1

Related Questions