sort values within groups pandas dataframe
import pandas as pd
df = pd.DataFrame({'A':[1, 2, 3, 4, 5, 6],
'B':[0, 1, 2, 0, 1, 2]})
df.groupby('B').apply(lambda x: x.sort_values('A', ascending = True))
sort values within groups pandas dataframe
import pandas as pd
df = pd.DataFrame({'A':[1, 2, 3, 4, 5, 6],
'B':[0, 1, 2, 0, 1, 2]})
df.groupby('B').apply(lambda x: x.sort_values('A', ascending = True))
groupby and sort python
In[34]: df.sort_values(['job','count'],ascending=False).groupby('job').head(3)
Out[35]:
count job source
4 7 sales E
2 6 sales C
1 4 sales B
5 5 market A
8 4 market D
6 3 market B
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us