【发布时间】:2016-11-17 23:03:21
【问题描述】:
This is the closest to what i'm looking for that I've found
假设我的数据框看起来像这样:
d = {'item_number':['K208UL','AKD098008','DF900A','K208UL','AKD098008']
'Comp_ID':['998798098','988797387','12398787','998798098','988797387']
'date':['2016-11-12','2016-11-13','2016-11-17','2016-11-13','2016-11-14']}
df = pd.DataFrame(data=d)
我想计算连续几天观察到相同item_number 和Comp_ID 的次数。
我想这看起来会是这样的:
g = df.groupby(['Comp_ID','item_number'])
g.apply(lambda x: x.loc[x.iloc[i,'date'].shift(-1) - x.iloc[i,'date'] == 1].count())
但是,在比较之前,我需要从每个日期中提取日期作为 int,我也遇到了麻烦。
for i in df.index:
wbc_seven.iloc[i, 'day_column'] = datetime.datetime.strptime(df.iloc[i,'date'],'%Y-%m-%d').day
显然基于位置的索引只允许整数?我该如何解决这个问题?
【问题讨论】: