3
這是my other question後續問題:計算第n天,從第一個事件各組內的大熊貓
我有以下的數據幀,從我的原始數據幀子集,與列OB, 事件,unixtime和天,我想補充另一列arbday這是自第一事件,對第n天(與第一次爲1天)由OB分組:
import numpy as np
import datetime as dt
>>> newdf = pd.DataFrame({'ob': ['a','a','b','b','c', 'd', 'e', 'e', 'e', 'f', 'f', 'f'],'event': [1, 2, 1, 2, 1, 1, 1, 2, 3, 1, 2, 3], 'unixtime': [1346682124716, 1346682188598, 1346745432765, 1347080641650, 1346676710509, 1346702995184, 1346530405978, 1346530421609, 1346530570952, 1346617885925, 1346961625305,1347214217566]},index=[343340, 343341, 343342, 343343, 343344, 343345, 343349, 343350, 343351, 343352,343353,343354])
>>> newdf['day'] = newdf['unixtime'].apply(lambda x: dt.datetime.utcfromtimestamp(x/1000).date())
ob event unixtime day arbday
343340 a 1 1346682124716 2012-09-03 1
343341 a 2 1346682188598 2012-09-03 1
343342 b 1 1346745432765 2012-09-04 1
343343 b 2 1347080641650 2012-09-08 5
343344 c 1 1346676710509 2012-09-03 1
343345 d 1 1346702995184 2012-09-03 1
343349 e 1 1346530405978 2012-09-01 1
343350 e 2 1346530421609 2012-09-01 1
343351 e 3 1346530570952 2012-09-01 1
343352 f 1 1346617885925 2012-09-02 1
343353 f 2 1346961625305 2012-09-06 5
343354 f 3 1347214217566 2012-09-09 8
在一個OB,這將工作:
newdf['arbday'] = newdf['day'].map(lambda x: (x-testdf.get_value(newdf[newdf.event == 1].first_valid_index(), 'day')).days+1)
或
newdf['arbday'] = newdf['day'].map(lambda x: (x-newdf.get_value(int(newdf[newdf.event == 1].index), 'day')).days+1)
我嘗試下面的代碼和它的工作:
>>> newdf['arbday'] = newdf.groupby('ob')['day'].transform(lambda x: (x-x.min()).apply(lambda y: y.days)+1)
event ob unixtime day arbday
343340 1 a 1346682124716 2012-09-03 1
343341 2 a 1346682188598 2012-09-03 1
343342 1 b 1346745432765 2012-09-04 1
343343 2 b 1347080641650 2012-09-08 5
343344 1 c 1346676710509 2012-09-03 1
343345 1 d 1346702995184 2012-09-03 1
343349 1 e 1346530405978 2012-09-01 1
343350 2 e 1346530421609 2012-09-01 1
343351 3 e 1346530570952 2012-09-01 1
343352 1 f 1346617885925 2012-09-02 1
343353 2 f 1346961625305 2012-09-06 5
343354 3 f 1347214217566 2012-09-09 8
但是,這是顯然不是最優雅的做法。另外,爲什麼事件和ob的順序發生了變化?
任何指針將不勝感激。謝謝!