сгруппировать по теме и свернуть столбец строк в соответствующие категории - PullRequest
0 голосов
/ 24 сентября 2018

Учитывая:

import pandas as pd

lis1= ('baseball', 'basketball', 'baseball', 'hockey', 'hockey', 'basketball')
lis2= ('I had lots of fun', 'This was the most boring sport', "I hit the ball hard", 'the puck went too fast', 'I scored a goal', 'the basket was broken')

pd.DataFrame({'topic':lis1, 'review':lis2})

        topic                          review
0    baseball               I had lots of fun
1  basketball  This was the most boring sport
2    baseball             I hit the ball hard
3      hockey          the puck went too fast
4      hockey                 I scored a goal
5  basketball           the basket was broken

Мне нужно это как pd.DataFrame:

lis1= ('baseball', 'basketball', 'hockey')
lis2= ("I had lots of fun, I hit the ball hard", "This was the most boring sport, the basket was broken","the puck went too fast I scored a goal")

pd.DataFrame({'topic':lis1, 'review':lis2})

        topic                                             review
0    baseball             I had lots of fun, I hit the ball hard
1  basketball  This was the most boring sport, the basket was...
2      hockey             the puck went too fast I scored a goal

Я запутался, потому что столбец, по которому я хочу сгруппировать, - это строка иЯ хотел бы объединить строки вместе.Строки не должны быть разделены запятой.

1 Ответ

0 голосов
/ 24 сентября 2018

Используйте groupby и объедините строки с помощью str.join:

df.groupby('topic', as_index=False).agg({'review' : ', '.join})

        topic                                             review
0    baseball             I had lots of fun, I hit the ball hard
1  basketball  This was the most boring sport, the basket was...
2      hockey            the puck went too fast, I scored a goal

Или, groupby и вызова apply, с немного другим синтаксисом:

df.groupby('topic')['review'].apply(', '.join).reset_index()

        topic                                             review
0    baseball             I had lots of fun, I hit the ball hard
1  basketball  This was the most boring sport, the basket was...
2      hockey            the puck went too fast, I scored a goal
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...