回答編集履歴
1
コード修正
test
CHANGED
@@ -2,20 +2,44 @@
|
|
2
2
|
|
3
3
|
import pandas as pd
|
4
4
|
|
5
|
+
import numpy as np
|
6
|
+
|
7
|
+
from io import StringIO
|
5
8
|
|
6
9
|
|
7
|
-
df = pd.DataFrame({'text':['あいう', 'エオ', 'かき']})
|
8
10
|
|
9
|
-
|
11
|
+
s = """Time,Spend,IDs,Text
|
10
12
|
|
11
|
-
|
13
|
+
11-Feb-2020 00:00,200,123,あいう
|
12
14
|
|
13
|
-
|
15
|
+
12-Feb-2020 00:00,100,456,エオ
|
14
16
|
|
15
|
-
|
17
|
+
13-Feb-2020 00:00,100,789,かき"""
|
16
18
|
|
17
|
-
|
19
|
+
df = pd.read_csv(StringIO(s))
|
18
20
|
|
21
|
+
|
22
|
+
|
23
|
+
# IDsがかぶった場合はとりあえず文字を連結する
|
24
|
+
|
25
|
+
grp = df.groupby('IDs').agg({'Spend':np.sum, 'Text':lambda col: ''.join(col)})
|
26
|
+
|
27
|
+
grp['Len'] = grp['Text'].str.len()
|
28
|
+
|
29
|
+
print(grp)
|
30
|
+
|
31
|
+
"""
|
32
|
+
|
33
|
+
Spend Text Len
|
34
|
+
|
35
|
+
IDs
|
36
|
+
|
37
|
+
123 200 あいう 3
|
38
|
+
|
39
|
+
456 100 エオ 2
|
40
|
+
|
19
|
-
|
41
|
+
789 100 かき 2
|
42
|
+
|
43
|
+
"""
|
20
44
|
|
21
45
|
```
|