質問編集履歴

1

エラー発生の追加

2019/08/09 03:02

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -61,3 +61,69 @@
61
61
  店頭のみ:同idでchannelカラムにstoreのみが存在する。
62
62
 
63
63
  webのみ:同IDでchannelカラムにwebのみが存在する。
64
+
65
+
66
+
67
+ ### 発生するエラー
68
+
69
+
70
+
71
+ ```python
72
+
73
+
74
+
75
+ pdf = pd.pivot_table(df.loc[:, ["no", "channel"]], index="no",
76
+
77
+ columns="channel", aggfunc=len)
78
+
79
+
80
+
81
+ # > print(pdf)
82
+
83
+ # channel store web
84
+
85
+ # no
86
+
87
+ # 1 1.0 2.0
88
+
89
+ # 6 2.0 NaN
90
+
91
+ # 22 NaN 2.0
92
+
93
+
94
+
95
+
96
+
97
+ def conv_func(row):
98
+
99
+ if row["store"] > 0 and row["web"] > 0:
100
+
101
+ return "どちらも利用"
102
+
103
+ elif row["store"] > 0:
104
+
105
+ return "店頭のみ"
106
+
107
+ elif row["web"] > 0:
108
+
109
+ return "Webのみ"
110
+
111
+ else:
112
+
113
+ raise RuntimeError(str(row))
114
+
115
+
116
+
117
+
118
+
119
+ id_main_channel_dict = dict(pdf.apply(conv_func, axis=1)) # dict: ID -> main_channel
120
+
121
+
122
+
123
+ out:
124
+
125
+ KeyError: ('web', 'occurred at index 07562705')
126
+
127
+
128
+
129
+ ```