user10508414
user10508414

Reputation:

How to combine groupby and sort values

How to merge two groupby and sort_values in to one

df_most_ordered = online_rt.groupby(by=['Country']).sum()

df_most_ordered.sort_values(['Quantity'],ascending=False).iloc[1:11]

Upvotes: 1

Views: 335

Answers (3)

Bharath_Raja
Bharath_Raja

Reputation: 642

You can perform the action in a same line by using the "."

df_most_ordered = online_rt.groupby(by=['Country']).sum().sort_values(['Quantity'],ascending=False).iloc[1:11]

Upvotes: 1

ignoring_gravity
ignoring_gravity

Reputation: 10476

You can use method chaining:

online_rt.groupby(by=["Country"]).sum().sort_values(
    ["Quantity"], ascending=False
).iloc[1:11]

Upvotes: 2

jezrael
jezrael

Reputation: 862511

Use . for chaining both rows to one:

online_rt.groupby(by=['Country']).sum().sort_values(['Quantity'],ascending=False).iloc[1:11]

Upvotes: 1

Related Questions