回答編集履歴
2
追記
test
CHANGED
@@ -49,3 +49,107 @@
|
|
49
49
|
|
50
50
|
|
51
51
|
[Pythonのif文による条件分岐の書き方](https://note.nkmk.me/python-if-elif-else/)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
---
|
56
|
+
|
57
|
+
【追記】
|
58
|
+
|
59
|
+
手元で試した結果を追記します。こちらでは問題なさそうです。
|
60
|
+
|
61
|
+
```Python
|
62
|
+
|
63
|
+
import pandas as pd
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
meal = pd.DataFrame({'meal_id':['1558','1778','1109','1885','1000','2581','1962','2290','2539','2707','1993','2664']})
|
68
|
+
|
69
|
+
print(meal)
|
70
|
+
|
71
|
+
# meal_id
|
72
|
+
|
73
|
+
#0 1558
|
74
|
+
|
75
|
+
#1 1778
|
76
|
+
|
77
|
+
#2 1109
|
78
|
+
|
79
|
+
#3 1885
|
80
|
+
|
81
|
+
#4 1000
|
82
|
+
|
83
|
+
#5 2581
|
84
|
+
|
85
|
+
#6 1962
|
86
|
+
|
87
|
+
#7 2290
|
88
|
+
|
89
|
+
#8 2539
|
90
|
+
|
91
|
+
#9 2707
|
92
|
+
|
93
|
+
#10 1993
|
94
|
+
|
95
|
+
#11 2664
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def meal_cluster(x):
|
100
|
+
|
101
|
+
if x["meal_id"] == "1558" or x["meal_id"] == "2581" or x["meal_id"] == "2707":
|
102
|
+
|
103
|
+
res = 1
|
104
|
+
|
105
|
+
elif x["meal_id"] == "1778" or x["meal_id"] == "1962" or x["meal_id"] == "1993":
|
106
|
+
|
107
|
+
res = 2
|
108
|
+
|
109
|
+
elif x["meal_id"] == "1109" or x["meal_id"] == "2290" or x["meal_id"] == "2664":
|
110
|
+
|
111
|
+
res = 3
|
112
|
+
|
113
|
+
elif x["meal_id"] == "1885" or x["meal_id"] == "2539":
|
114
|
+
|
115
|
+
res = 4
|
116
|
+
|
117
|
+
else:
|
118
|
+
|
119
|
+
res = 5
|
120
|
+
|
121
|
+
return res
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
meal["meal_cluster"] = meal.apply(meal_cluster, axis=1)
|
126
|
+
|
127
|
+
print(meal)
|
128
|
+
|
129
|
+
# meal_id meal_cluster
|
130
|
+
|
131
|
+
#0 1558 1
|
132
|
+
|
133
|
+
#1 1778 2
|
134
|
+
|
135
|
+
#2 1109 3
|
136
|
+
|
137
|
+
#3 1885 4
|
138
|
+
|
139
|
+
#4 1000 5
|
140
|
+
|
141
|
+
#5 2581 1
|
142
|
+
|
143
|
+
#6 1962 2
|
144
|
+
|
145
|
+
#7 2290 3
|
146
|
+
|
147
|
+
#8 2539 4
|
148
|
+
|
149
|
+
#9 2707 1
|
150
|
+
|
151
|
+
#10 1993 2
|
152
|
+
|
153
|
+
#11 2664 3
|
154
|
+
|
155
|
+
```
|
1
訂正
test
CHANGED
@@ -42,7 +42,7 @@
|
|
42
42
|
|
43
43
|
|
44
44
|
|
45
|
-
条件式を修正した場合でも、elifを使用しないとresが4と5のみになってしま
|
45
|
+
条件式を修正した場合でも、elifを使用しないとresが4と5のみになってしまいます。
|
46
46
|
|
47
47
|
ifの使い方と条件式の書き方を学びましょう。
|
48
48
|
|