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:

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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With