回答編集履歴
1
コード追加
test
CHANGED
@@ -43,3 +43,73 @@
|
|
43
43
|
df
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
user_id別ver.
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```python3
|
54
|
+
|
55
|
+
import pandas as pd
|
56
|
+
|
57
|
+
import copy
|
58
|
+
|
59
|
+
dfa = pd.DataFrame({
|
60
|
+
|
61
|
+
'action_id':['1', '2', '3', '4', '5', '6', '7', '8'],
|
62
|
+
|
63
|
+
'user_id':['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
|
64
|
+
|
65
|
+
'date':['2020-06-05', '2020-06-06', '2020-06-08', '2020-06-10', '2020-06-11', '2020-06-12', '2020-06-15', '2020-06-20'],
|
66
|
+
|
67
|
+
'actoin_type':['get', 'purchase', 'purchase', 'purchase', 'get', 'purchase', 'get', 'purchase'],
|
68
|
+
|
69
|
+
'point_get':['500', '0', '0', '0', '300', '0', '200', '0'],
|
70
|
+
|
71
|
+
'point_use':['0', '300', '0', '500','0', '250', '0', '150']
|
72
|
+
|
73
|
+
})
|
74
|
+
|
75
|
+
dfb = pd.DataFrame({
|
76
|
+
|
77
|
+
'action_id':['1', '2', '3', '4', '5', '6', '7', '8'],
|
78
|
+
|
79
|
+
'user_id':['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'],
|
80
|
+
|
81
|
+
'date':['2020-06-05', '2020-06-06', '2020-06-08', '2020-06-10', '2020-06-11', '2020-06-12', '2020-06-15', '2020-06-20'],
|
82
|
+
|
83
|
+
'actoin_type':['get', 'purchase', 'purchase', 'purchase', 'get', 'purchase', 'get', 'purchase'],
|
84
|
+
|
85
|
+
'point_get':['300', '0', '100', '0', '300', '0', '200', '0'],
|
86
|
+
|
87
|
+
'point_use':['0', '300', '0', '500','0', '250', '0', '150']
|
88
|
+
|
89
|
+
})
|
90
|
+
|
91
|
+
df=pd.concat([dfa,dfb])
|
92
|
+
|
93
|
+
df = df.reset_index(drop=True)
|
94
|
+
|
95
|
+
blc = []
|
96
|
+
|
97
|
+
for user_id, group in df.groupby("user_id"):
|
98
|
+
|
99
|
+
record = 0
|
100
|
+
|
101
|
+
for gt,us in zip(group["point_get"],group["point_use"]):
|
102
|
+
|
103
|
+
record += int(gt) -int(us)
|
104
|
+
|
105
|
+
if record < 0:
|
106
|
+
|
107
|
+
record = 0
|
108
|
+
|
109
|
+
blc.append(record)
|
110
|
+
|
111
|
+
df["balance"] = pd.DataFrame(blc)
|
112
|
+
|
113
|
+
df
|
114
|
+
|
115
|
+
```
|