Reputation: 1857
I have a DataFrame named a
. I want to get the top most used time apps.
import pandas as pd
a=pd.DataFrame({'user':[1,1,1,2,2,2,2],'app':['k','p','s','k','p','s','t'],'time':[5,10,15,10,5,3,1]})
Input:
user app time
0 1 k 5
1 1 p 10
2 1 s 15
3 2 k 10
4 2 p 5
5 2 s 3
6 2 t 1
For example, I want to get the top two most used apps
according the column time
. I expect the output as follows.
Expected:
user top1_app top2_app
0 1 s p
1 2 k p
As you see, user 1
has the longest time to use the app
called s
, and has the second longest time to use the app
called p
.
Hopefully for help and thanks!
Upvotes: 2
Views: 59
Reputation: 38415
You can rank the time column and then reshape
a['time1'] = a.groupby('user').time.rank(method = 'dense', ascending = False).map({1.0 : 'top1_app', 2.0 : 'top2_app'})
a = a.dropna().pivot('user', 'time1', 'app')
a.columns.name = None
a.reset_index(inplace = True)
user top1_app top2_app
0 1 s p
1 2 k p
Upvotes: 3