python - Pandas: Period object to abstract from time -
i have following dataframe:
df = pd.dataframe({ 'trader': 'carl mark carl joe mark carl max max'.split(), 'share': list('abaabaaa'), 'quantity': [5,2,5,10,1,5,2,1] }, index=[ dt.datetime(2013,1,1,13,0), dt.datetime(2013,1,1,13,5), dt.datetime(2013,1,1,20,0), dt.datetime(2013,1,2,10,0), dt.datetime(2013,1,2,12,0), dt.datetime(2013,1,2,14,0), dt.datetime(2013,6,2,14,0), dt.datetime(2013,7,2,14,0), ])
is possible create period object on daily basis abstracts concrete day. evaluate question whether there tendency among traders in sample trade lower volumes.
to create table this:
period | trader | quantity -------------------------- 1 | carl | 10 1 | mark | 2 1 | joe | 10 1 | max | 2 2 | carl | 5 2 | mark | 1 2 | max | 1
andy
update:
the datasampel above simple show problem. hope create period object abstracts concrete date. goal compare sequence of occurred trades per trader.
df1 = pd.dataframe({ 'trader': 'carl mark carl joe mark carl max max'.split(), 'share': list('abaabaaa'), 'quantity': [5,2,5,10,1,5,2,1] }, index=[ dt.datetime(2013,1,1,13,0), dt.datetime(2013,1,1,13,5), dt.datetime(2013,1,1,20,0), dt.datetime(2013,2,6,10,0), dt.datetime(2013,2,5,12,0), dt.datetime(2013,3,7,14,0), dt.datetime(2013,6,4,14,0), dt.datetime(2013,7,4,14,0), ])
this table
in [22]: x = df.reset_index() in [23]: x['day'] = x['index'].apply(lambda x: x.day) in [24]: x out[24]: index quantity share trader day 0 2013-01-01 13:00:00 5 carl 1 1 2013-01-01 13:05:00 2 b mark 1 2 2013-01-01 20:00:00 5 carl 1 3 2013-01-02 10:00:00 10 joe 2 4 2013-01-02 12:00:00 1 b mark 2 5 2013-01-02 14:00:00 5 carl 2 6 2013-06-02 14:00:00 2 max 2 7 2013-07-02 14:00:00 1 max 2
but want
in [25]: x.groupby(['day','trader']).sum() out[25]: quantity day trader 1 carl 10 mark 2 2 carl 5 joe 10 mark 1 max 3
Comments
Post a Comment