Skip to content
Advertisement

How to sort aggregated numpy array?

My first post on stackoverflow + am very new to programming. Apologies in advance for any poor formatting and missing information. :)

I aggregated two columns in a csv file (one column of seller names, the other of transactional amounts) to find how much each seller has made in total:

seller_group = df.groupby(["seller"])
#Total amount each seller has made so far
seller_group.aggregate({'price_paid':np.sum})

I want to sort it in descending order to find who the top sellers are, so I tried this:

to_sort = seller_group.aggregate({'price_paid':np.sum})
to_sort(x)[::-1]

The result is a numpy series without the seller column and the prices are formatted strangely (for example, [5.00000e+00]). I’d like a result that looks like this, but with the price_paid column sorted: seller sums output

Any ideas?

Advertisement

Answer

Try .sort_values():

df_out = seller_group.aggregate({'price_paid':np.sum}).sort_values(by="price_paid", ascending=False)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement