回答編集履歴
1
別解を追記
test
CHANGED
@@ -45,3 +45,69 @@
|
|
45
45
|
"""
|
46
46
|
|
47
47
|
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
#### 別解:先頭から舐める版
|
52
|
+
|
53
|
+
ユニークidが多く、各id毎の行数が少ない場合は、以下のように先頭から舐めて計算する方が速いかもしれません。
|
54
|
+
|
55
|
+
```Python
|
56
|
+
|
57
|
+
import pandas as pd
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
df = pd.DataFrame({'id':['a','a','a','b','b','b'],'date':['2018/01/23','2018/01/24','2018/01/26','2018/01/23','2018/01/26','2018/01/30']},
|
62
|
+
|
63
|
+
columns = ['id','date'])
|
64
|
+
|
65
|
+
df.loc[:,'date'] = pd.to_datetime(df['date'])
|
66
|
+
|
67
|
+
df['delta'] = 0
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
prev_id,prev_date = df.loc[0,'id'], df.loc[0,'date']
|
72
|
+
|
73
|
+
for idx,row in df.iterrows():
|
74
|
+
|
75
|
+
cur_id = row['id']
|
76
|
+
|
77
|
+
cur_date = row['date']
|
78
|
+
|
79
|
+
if prev_id != cur_id:
|
80
|
+
|
81
|
+
pass
|
82
|
+
|
83
|
+
else:
|
84
|
+
|
85
|
+
df.loc[idx,'delta'] = cur_date - prev_date
|
86
|
+
|
87
|
+
prev_id = cur_id
|
88
|
+
|
89
|
+
prev_date = cur_date
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
print(df)
|
94
|
+
|
95
|
+
"""
|
96
|
+
|
97
|
+
id date delta
|
98
|
+
|
99
|
+
0 a 2018-01-23 0 days 00:00:00
|
100
|
+
|
101
|
+
1 a 2018-01-24 1 days 00:00:00
|
102
|
+
|
103
|
+
2 a 2018-01-26 2 days 00:00:00
|
104
|
+
|
105
|
+
3 b 2018-01-23 0
|
106
|
+
|
107
|
+
4 b 2018-01-26 3 days 00:00:00
|
108
|
+
|
109
|
+
5 b 2018-01-30 4 days 00:00:00
|
110
|
+
|
111
|
+
"""
|
112
|
+
|
113
|
+
```
|