I have the following dataframe:
Date A B
=====================
2015-01-01 A 0
2015-01-02 A 1
2015-01-03 A 0
2015-01-01 B 0
2015-01-02 B 0
2015-01-03 B 0
2015-01-04 B 1
2015-01-05 B 1
Require:
Date A B C
===========================
2015-01-01 A 0 0
2015-01-02 A 1 01
2015-01-03 A 0 010
2015-01-01 B 0 0
2015-01-02 B 0 00
2015-01-03 B 0 000
2015-01-04 B 1 0001
2015-01-05 B 1 00011
Column C is derived for 2015-01-03 by taking previous value of B.
This is the best I got so far, but it appends all values within a group. Previous attempts with shifting wasn't successful for me.
df2 = df.groupby('Date', 'A')['B'].apply(''.join).reset_index()
df = df.merge(df2, on=['Date', 'A'], how='left')