I have table pulled from sqlite3 using sqlalchemy. This table holds the date and time of each showing of the car:

```
Id  Car     Code    ShowTime
1   Honda    A      10/18/2017 14:45
1   Honda    A      10/18/2017 17:10
3   Honda    C      10/18/2017 19:35
4   Toyota   B      10/18/2017 12:20
4   Toyota   B      10/18/2017 14:45

```

What would be the desired output is to seperate the date and put each timestamps on a list object:

```
"data":{
        'id': '1',
        'schedule': {
            'car': 'Honda',
            'show_date': '10/18/2017',
            'time_available': [
                '14:45',
                '17:10',        
            ],
            'code': 'A'

        }
    },{
        'id': '3',
        'schedule': {
            'car': 'Honda',
            'show_date': '10/18/2017',
            'time_available': [
                '19:35'
            ],
            'code': 'C'
        }
    },{
        'id': '4',
        'schedule': {
            'car': 'Toyota',
            'show_date': '10/18/2017',
            'time_available': [
                '12:20',
                '14:45'
            ],
            'code': 'B'
        }
    }

```

Any help is greatly appreciated!
['https://Stackoverflow.com/questions/56801384', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2341389/']
You can use pandas to split the `ShowTime` column:

```
In [22]: import pandas as pd

In [68]: df = pd.read_csv('test.csv')

In [69]: df.rename(columns={'Id':'id','Car':'car', 'Code':'code'}, inplace=True)

In [70]: df[['show_date', 'time_available']] = df.ShowTime.str.split(' ', expand=True)

In [71]: df.drop('ShowTime', axis=1, inplace=True)

In [72]: df
Out[72]:
   id     car code   show_date time_available
0   1   Honda    A  10/18/2017          14:45
1   1   Honda    A  10/18/2017          17:10
2   3   Honda    C  10/18/2017          19:35
3   4  Toyota    B  10/18/2017          12:20
4   4  Toyota    B  10/18/2017          14:45

```

`groupby` columns with categorical values and convert 'time\_available' column to list on the grouped dataframe:

```
In [134]: df_grp = df.groupby(['id', 'car','code', 'show_date'])

In [136]: df_grp_time_stacked = df_grp['time_available'].apply(list).reset_index()

In [138]: df_grp_time_stacked
Out[138]:
   id     car code   show_date  time_available
0   1   Honda    A  10/18/2017  [14:45, 17:10]
1   3   Honda    C  10/18/2017         [19:35]
2   4  Toyota    B  10/18/2017  [12:20, 14:45]

In [139]: df_grp_time_stacked['time_available'] = df_grp_time_stacked['time_available'].apply(lambda x:x[0] if (len(x)=
     ...: =1) else x)

In [140]: df_grp_time_stacked
Out[140]:
   id     car code   show_date  time_available
0   1   Honda    A  10/18/2017  [14:45, 17:10]
1   3   Honda    C  10/18/2017           19:35
2   4  Toyota    B  10/18/2017  [12:20, 14:45]

```

Now convert the dataframe to dict:

```
In [165]: raw_dict = df_grp_time_stacked.to_dict(orient='records')

In [166]: data = {'data':raw_dict}

In [167]: data
Out[167]:
{'data': [{'id': 1,
   'car': 'Honda',
   'code': 'A',
   'show_date': '10/18/2017',
   'time_available': ['14:45', '17:10']},
  {'id': 3,
   'car': 'Honda',
   'code': 'C',
   'show_date': '10/18/2017',
   'time_available': '19:35'},
  {'id': 4,
   'car': 'Toyota',
   'code': 'B',
   'show_date': '10/18/2017',
   'time_available': ['12:20', '14:45']}]}

```
You can use the simple setdefault() dictionary method, too:

```
tbl=['1   Honda    A      10/18/2017 14:45',
     '1   Honda    A      10/18/2017 17:10', 
     '3   Honda    C      10/18/2017 19:35', 
     '4   Toyota   B      10/18/2017 12:20', 
     '4   Toyota   B      10/18/2017 14:45']

data={}                                                                                                               

for line in tbl: 

    iden,car,code,show_date,time_available= line.split() 

    data.setdefault( (iden,car,code), {'id':iden,'schedule': {'car':car,'show_date':show_date,'time_available':[],'code':code}})['schedule']['time_available'].append(time_available); 

```

We use the (iden,car,code) tuple as dictionary key. The 'setdefault' gets the value of the key if it is exists in the dict, if not, then creates and puts a default value in it. We create the default structure with an empty "time\_available' list, and as 'setdefault' returns the existing or the newly created value, we address that list and append the time value to.
The result:

```
data.values()                                                                                                         
dict_values([{'id': '1', 'schedule': {'car': 'Honda', 'show_date': '10/18/2017', 'time_available': ['14:45', '17:10'], 'code': 'A'}}, {'id': '3', 'schedule': {'car': 'Honda', 'show_date': '10/18/2017', 'time_available': ['19:35'], 'code': 'C'}}, {'id': '4', 'schedule': {'car': 'Toyota', 'show_date': '10/18/2017', 'time_available': ['12:20', '14:45'], 'code': 'B'}}])

```