質問編集履歴

1

完成コード追記

2018/10/16 12:54

投稿

Yukiya025
Yukiya025

スコア86

test CHANGED
File without changes
test CHANGED
@@ -149,3 +149,91 @@
149
149
 
150
150
 
151
151
  よろしくお願いしますm(__)m
152
+
153
+
154
+
155
+ # できましたー(≧∇≦)b
156
+
157
+ [mkgrei](https://teratail.com/users/mkgrei)さまのアドバイスのおかげです<3
158
+
159
+ `header = 'user_id'`だけで`user_id,comment`の両方がヘッダーに入る理由がわかりませんが、最終的に望む挙動になったので嬉しいです(*^^*)
160
+
161
+ ```
162
+
163
+ def del_dup():
164
+
165
+ on_en = pd.read_csv('OnlyEn.csv')
166
+
167
+ sorted_onen = on_en.sort_values(['user_id', 'comment'],
168
+
169
+ ascending=[1, 0])
170
+
171
+ # print(sorted_onen)
172
+
173
+ # print(len(on_en))
174
+
175
+
176
+
177
+ df = pd.DataFrame(sorted_onen)
178
+
179
+ print(df)
180
+
181
+ ans = df.groupby('user_id')['comment'].apply(lambda x: ','.join(x))
182
+
183
+ print(ans)
184
+
185
+
186
+
187
+ ans.to_csv('final.csv', header = 'user_id')
188
+
189
+
190
+
191
+
192
+
193
+ del_dup()
194
+
195
+ ```
196
+
197
+
198
+
199
+ ```
200
+
201
+ # OnlyEn.csv
202
+
203
+ user_id,comment
204
+
205
+ 1,Hi
206
+
207
+ 2,World
208
+
209
+ 3,Hello
210
+
211
+ 4,Python
212
+
213
+ 3,Sato
214
+
215
+ 4,class
216
+
217
+ 3,find
218
+
219
+ 2,alice
220
+
221
+ ```
222
+
223
+
224
+
225
+ ```
226
+
227
+ # final.csv
228
+
229
+ user_id,comment
230
+
231
+ 1,Hi
232
+
233
+ 2,"alice,World"
234
+
235
+ 3,"find,Sato,Hello"
236
+
237
+ 4,"class,Python"
238
+
239
+ ```