Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas value counts without sorting results

I am using Python's value_counts(sort=False) function on a column called 'order_id' on my dataframe, but the order of output is different from the order the data is displayed in the dataframe.

For instance, when I do df['order_id'].value_counts(sort=False), the result order is different from the order in the dataframe (2398795,473747) etc.

The dataframe looks like this:

enter image description here

The end goal is this: For each order id, I want the count of product_ids for that order and the days_since prior order.

like image 368
creoleChe Avatar asked Nov 22 '25 15:11

creoleChe


1 Answers

IIUC, use groupby and agg:

df.groupby('order_id', sort=False)\
  .agg({'product_id': 'size','days_since_prior_order': 'sum'})

Output:

          product_id  days_since_prior_order
order_id                                    
2398795            6                    90.0
473747             5                   105.0
2254736            5                   145.0
431534             2                    56.0
like image 155
Scott Boston Avatar answered Nov 25 '25 05:11

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!