1

I have a Pandas DataFrame like this:

import pandas as pd
df = pd.DataFrame({'hour': pd.Series(['17:00', '18:00', '19:00', '20:00']),
                   'url': pd.Series(['url1', 'url2', 'url3', 'url4']),
                   'string': pd.Series(['my name', 'your name', 'my name', 'your name'])})

    hour   url     string
0  17:00  url1    my name
1  18:00  url2  your name
2  19:00  url3    my name
3  20:00  url4  your name

and I have to get some like this:

       string          hour               url
0     my name     '17:00','19:00'      url1, url3
1     your name   '18:00','20:00'      url2, url4

Is there a way to do this transform?

smci
  • 32,567
  • 20
  • 113
  • 146
DiegoT
  • 29
  • 6
  • 6
    `df.groupby('string').agg(list)`? – Quang Hoang May 19 '20 at 19:38
  • 2
    `df.groupby('string').agg(', '.join).reset_index()`? – Scott Boston May 19 '20 at 19:40
  • *"group by a column"* is called ***`groupby`***, and *"group/process duplicated values together"* is typically an ***`aggregate`***, in your case *"aggregate into a list"*. Please briefly skim the ["10 minutes to pandas" tutorial](https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html) so you learn the important terms, it will help you phrase questions. – smci May 19 '20 at 20:22

0 Answers0