我有这本字典

example = {

'view_id_ga_standard_111': {'view_id': '111',
  'request_type': 'ga_standard',
  'start_date': '2019-07-01',
  'end_date': '2019-09-01',
  'status': 'New'},

'view_id_ga_standard_333': {'view_id': '333',
  'request_type': 'ga_standard',
  'start_date': '2019-07-01',
  'end_date': '2019-09-01',
  'status': 'New'},

'view_id_ga_corporate_222': {'view_id': '222',
  'request_type': 'ga_corporate',
  'start_date': '2018-07-01',
  'end_date': '2018-09-01',
  'status': 'New'}
}




并且需要用它制作一个熊猫df,所以看起来像这样

    id  request_type start_date  end_date   request_id  status

2   111 ga_standard  2019-07-01 2019-09-01  1           New
3   333 ga_standard  2019-07-01 2019-09-01  2           New
5   222 ga_corporate 2018-07-01 2018-09-01  3           New


我结束了这个功能

def ga_make_request_types(params):
    vids = []
    rtypes = []
    sdates = []
    edates = []
    js = []
    statuses = []
    j = 0


    for k,v in data_for_config.items():
        j = j + 1
        view_id = v['view_id']
        vids.append(view_id)
        request_type = v['request_type']
        rtypes.append(request_type)
        start_date = v['start_date']
        sdates.append(start_date)
        end_date = v['end_date']
        edates.append(end_date)
        status = v['status']
        statuses.append(status)
        js.append(j)

    df = pd.DataFrame(zip(vids, rtypes, sdates, edates, js, statuses), columns=['id', 'request_type', 'start_date', 'end_date', 'request_id', 'status'])

    return df


但这很丑陋,是否可以通过列表理解来缩短代码?

我已经尝试过这样

for k,v in data_for_config.items():
   vids = [v['view_id'] for v in k]


像这样

for k,v in data_for_config.items():
   vids = [v['view_id'] for v in data_for_config[k]]


但这会引发错误

TypeError: string indices must be integers

最佳答案

怎么样:

df = pd.DataFrame(example).T


输出:

                            end_date  request_type  start_date status view_id
view_id_ga_corporate_222  2018-09-01  ga_corporate  2018-07-01    New     222
view_id_ga_standard_111   2019-09-01   ga_standard  2019-07-01    New     111
view_id_ga_standard_333   2019-09-01   ga_standard  2019-07-01    New     333


编辑:

我不确定如何确定request_id,但是您可以执行以下操作:

df['request_id'] = range(1, df.shape[0] + 1)

关于python - 如何优化代码以遍历字典并将值存储在列表中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58604958/

10-10 13:39