如何合并多个唯一ID

时间:2019-06-28 11:32:27

标签: python sqlite

我有一个类似于以下的数据:

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

如果我包含唯一的ID,下面的代码将返回多个实例输出:

all_car_schedules = db.session.query(Schedules.id, Schedules.code,
                                      Car.carname, Schedules.showtime) \
                               .filter(Schedules.id == Car.id)

df = pd.read_sql(all_car_schedules.statement, db.session.bind)

df[['show_date', 'start_times', 'median']] = df.showtime.str.split(' ', expand=True)
df['start_times'] = df['start_times'] + df['median']
df.drop('screening', axis=1, inplace=True)
df.drop('median', axis=1, inplace=True)
df_grp = df.groupby(['id', 'code', 'carname'])
df_grp_time_stacked = df_grp['start_times'].apply(list).reset_index()
df_grp_time_stacked['start_times'] = df_grp_time_stacked['start_times'].apply(lambda x: x[0] if (len(x) == 1) else x)
return_to_dict = df_grp_time_stacked.to_dict(orient='records')

上面的代码在预期输出应为以下情况时返回多行:

"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'
        }
    }

我也在使用sqlite3作为数据库。我不确定查询中是否应该进行更改。请让我知道您的想法,并为此提供帮助。非常感谢。我还使用sqlite3作为数据库。

1 个答案:

答案 0 :(得分:1)

您可以将Topic:__consumer_offsets PartitionCount:50 ReplicationFactor:3 Configs:segment.bytes=1138822,cleanup.policy=compact,compression.type=producer Topic: __consumer_offsets Partition: 0 Leader: -1 Replicas: 1000,1002,1001 Isr: Topic: __consumer_offsets Partition: 1 Leader: -1 Replicas: 1000,1002,1001 Isr: Topic: __consumer_offsets Partition: 2 Leader: -1 Replicas: 1000,1002,1001 Isr: Topic: __consumer_offsets Partition: 3 Leader: -1 Replicas: 1000,1002,1001 Isr: Topic: __consumer_offsets Partition: 4 Leader: -1 Replicas: 1000,1002,1001 Isr: Topic:gen_topic_totCount:100 ReplicationFactor:3 Configs: Topic: gen_topic_tot: 0 Leader: -1 Replicas: 1002,1000,1001 Isr: Topic: gen_topic_tot: 1 Leader: -1 Replicas: 1000,1001,1002 Isr: Topic: gen_topic_tot: 2 Leader: -1 Replicas: 1001,1002,1000 Isr: 函数与groupby()选项结合使用:

list

输出:

df = pd.DataFrame({'Id' : [1,2,3,4,5], 'Car': ['Honda', 'Honda', 'Honda', 'Toyota', 'Toyota'],
                    'Code': ['A', 'A', 'B', 'C', 'C'], 'show date': ['10/18/2017', '10/18/2017',
                                                                     '10/18/2017', '10/18/2017', '10/18/2017'],
                   'start_times' : ['14:45', '17:10', '19:35', '12:20', '14:45']})

df.groupby(['Car', 'Code', 'show date'])['start_times'].apply(list)

如果要保留第一个ID,则必须像这样将选项 start_times Car Code show date Honda A 10/18/2017 [14:45, 17:10] B 10/18/2017 [19:35] Toyota C 10/18/2017 [12:20, 14:45] 添加到ID行:

'first'