質問編集履歴
1
エラー発生の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -29,4 +29,37 @@
|
|
29
29
|
|
30
30
|
どちらも利用:同idでchannelカラムにweb,storeが存在する。
|
31
31
|
店頭のみ:同idでchannelカラムにstoreのみが存在する。
|
32
|
-
webのみ:同IDでchannelカラムにwebのみが存在する。
|
32
|
+
webのみ:同IDでchannelカラムにwebのみが存在する。
|
33
|
+
|
34
|
+
### 発生するエラー
|
35
|
+
|
36
|
+
```python
|
37
|
+
|
38
|
+
pdf = pd.pivot_table(df.loc[:, ["no", "channel"]], index="no",
|
39
|
+
columns="channel", aggfunc=len)
|
40
|
+
|
41
|
+
# > print(pdf)
|
42
|
+
# channel store web
|
43
|
+
# no
|
44
|
+
# 1 1.0 2.0
|
45
|
+
# 6 2.0 NaN
|
46
|
+
# 22 NaN 2.0
|
47
|
+
|
48
|
+
|
49
|
+
def conv_func(row):
|
50
|
+
if row["store"] > 0 and row["web"] > 0:
|
51
|
+
return "どちらも利用"
|
52
|
+
elif row["store"] > 0:
|
53
|
+
return "店頭のみ"
|
54
|
+
elif row["web"] > 0:
|
55
|
+
return "Webのみ"
|
56
|
+
else:
|
57
|
+
raise RuntimeError(str(row))
|
58
|
+
|
59
|
+
|
60
|
+
id_main_channel_dict = dict(pdf.apply(conv_func, axis=1)) # dict: ID -> main_channel
|
61
|
+
|
62
|
+
out:
|
63
|
+
KeyError: ('web', 'occurred at index 07562705')
|
64
|
+
|
65
|
+
```
|