How to create lag columns using grouby

20 views
Skip to first unread message

Panda Lover

unread,
Jul 21, 2016, 5:12:59 PM7/21/16
to PyData
Hi All,

I have a data frame looks like this:

C, Week, Quantity
a, 1, 3
a, 2, 4
a, 6, 7
b, 2, 4,
b, 3. 5
b. 5, 6

I would like to group the data frame by ['C1','C2'] and then use week and quantity information to compute lag information.


C, Week, Quantity, Lag
a, 1, 3 , NA
a, 2, 4,  3
a, 6, 7,  NA
b, 2, 4, NA
b, 3. 5, 4
b. 4, 6, 5.
b, 5, 8, NA

Can I use groupby's apply function to do this?
Or is there a efficient way to do this?

Thanks in advance,

Tom Augspurger

unread,
Jul 21, 2016, 5:37:34 PM7/21/16
to pyd...@googlegroups.com
StackOverflow is typically better for these types of questions, since it's easier for others to find. Does something like this solve your problem?

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages