PANDAS从起始位置连续计算连续日期

时间:2016-11-23 23:43:55

标签: python pandas

这是我正在使用的数据框的示例:

 d = {'item_number':['bdsm1000', 'bdsm1000', 'bdsm1000', 'ZZRWB18','ZZRWB18', 'ZZRWB18', 'ZZRWB18', 'ZZHP1427BLK', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1427', 'ZZHP1414', 'ZZHP1414', 'ZZHP1414', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115WNTR', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE', 'WRM115SCFRE'], 'Comp_ID':, [2454, 2454, 2454, 1395, 1395, 1395, 1395, 3378, 1266941, 660867, 43978, 1266941, 660867, 43978, 1266941, 660867, 43978, 1266941, 660867, 43978, 43978, 43978, 43978, 1197347907, 70745, 4737, 1197347907, 4737, 1197347907, 70745, 4737, 1197347907, 70745, 4737, 1197347907, 4737, 1197487704, 1197347907, 70745, 23872, 4737, 1197347907, 4737, 1197487704, 1197347907, 23872, 4737, 1197487704, 1197347907, 70745]
'date': ['2016-11-22', '2016-11-20', '2016-11-19', '2016-11-22', '2016-11-20', '2016-11-19', '2016-11-18', '2016-11-22', '2016-11-22', '2016-11-22', '2016-11-22', '2016-11-20', '2016-11-20', '2016-11-20', '2016-11-19', '2016-11-19', '2016-11-19', '2016-11-18', '2016-11-18', '2016-11-18', '2016-11-22', '2016-11-20', '2016-11-19', '2016-11-22', '2016-11-22', '2016-11-22', '2016-11-21', '2016-11-21', '2016-11-20', '2016-11-20', '2016-11-20', '2016-11-19', '2016-11-19', '2016-11-19', '2016-11-18', '2016-11-18', '2016-11-22', '2016-11-22', '2016-11-22', '2016-11-22', '2016-11-22', '2016-11-21', '2016-11-21', '2016-11-20', '2016-11-20', '2016-11-20', '2016-11-20', '2016-11-19', '2016-11-19', '2016-11-19']}

df = pd.DataFrame(data=d)

我想计算从2016-11-22开始的连续观察,按照Comp_ID和item_number进行分组。

基本上,我要做的是,计算连续多少天有一个观察值从今天开始计算每个Comp_ID和item_number。 (这个例子是在11月22日放在一起的)在今天之前几天/几天观察到的连续观察是不相关的。只有像今天这样的序列......昨天......前天......等等都是相关的。

我让这个工作在一个较小的样本上,但它似乎在更大的数据集上被绊倒。

d = {'item_number':['KIN005','KIN005','KIN005','KIN005','KIN005','A789B','A789B','A789B','G123    H','G123H','G123H'],
'Comp_ID':['1395','1395','1395','1395','1395','7787','7787','7787','1395','1395','1395'],
'date':['2016-11-22','2016-11-21','2016-11-20','2016-11-14','2016-11-13','2016-11-22','2016-11-21','2016-11-12','2016-11-22','2016-11-21','2016-11-08']}

df = pd.DataFrame(data=d)
df.date = pd.to_datetime(df.date)
d = pd.Timedelta(1, 'D')

df = df.sort_values(['item_number','date','Comp_ID'],ascending=False)

g = df.groupby(['Comp_ID','item_number'])
sequence = g['date'].apply(lambda x: x.diff().fillna(0).abs().le(d)).reset_index()
sequence.set_index('index',inplace=True)
test = df.join(sequence)
test.columns = ['Comp_ID','date','item_number','consecutive']
g = test.groupby(['Comp_ID','item_number'])
g['consecutive'].apply(lambda x: x.idxmin() - x.idxmax() )

这为以下数据集获得了所需的结果:

Comp_ID  item_number
1395     G123H          2
         KIN005         3
7787     KIN005         2
Name: consecutive, dtype: int64

2 个答案:

答案 0 :(得分:2)

<强> 设置
修复了你的数据

d = {'item_number':['KIN005','KIN005','KIN005','KIN005','KIN005'],
 'Comp_ID':['1395','1395','1395','1395','1395'],
 'date':['2016-11-22','2016-11-21','2016-11-20','2016-11-14','2016-11-13']}

df = pd.DataFrame(data=d)

df.date = pd.to_datetime(df.date)

解决方案

d = pd.Timedelta(1, 'D')
df = df.sort_values('date', ascending=False)
consecutive = df.date.diff().fillna(0).abs().le(d)
idx_loc = df.index.get_loc(consecutive.idxmin())
df.iloc[:idx_loc]

enter image description here

你最近的连续数是

idx_loc - 1

2

答案 1 :(得分:0)

看起来你有一些很好的功能方法,可能适合你。但如果你想要丑陋......我可以给你丑陋。

date = df.iloc[0, 1]
count = 0

consecutive_obs = []
for i, row in enumerate(df.values):

    if row[1] == date - datetime.timedelta(days=1):
        print(row)
        count += 1
    else:
        if i > 0:
            consecutive_obs = consecutive_obs + [count]*(count + 1)
            count = 0        
    date = row[1]

consecutive_obs = consecutive_obs + [count]*(count + 1)

df['consecutive_obs'] = consecutive_obs