KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ kdb equivalent of pandas groupby Re: kdb equivalent of pandas groupby

  • davidcrossey

    Member
    November 22, 2021 at 12:00 am

    Hi powerpeanuts,

    I would keep it simple as you have it, and change select to update in your example as follows:

    q)update change:0^price-prev[price] by sym from trade 
    time       sym price size change 
    ----------------------------------- 
    09:30:00.000 a 10.75  100 0 
    09:31:00.000 a 11.75  100 1 
    09:32:00.000 a 13.2   100 1.45 
    09:30:00.000 b 100.75 100 0 
    09:31:00.000 b 106.95 100 6.2 
    09:32:00.000 b 123.95 100 17

    Note, I’ve also used fill (^) here to set the first change value for each sym as 0; this is optional and you can leave it as null if you prefer.

    Hope this helps and answers your question.

    Kind regards,

    David