python - Pandas value counts without sorting results -


i using python's value_counts(sort=false) function on column called 'order_id' on dataframe, order of output different order data displayed in dataframe.

for instance, when df['order_id'].value_counts(sort=false), result order different order in dataframe (2398795,473747) etc.

the dataframe looks this:

enter image description here

the end goal this: each order id, want count of product_ids order , days_since prior order.

iiuc, use groupby , 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 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -