【问题标题】:Populate dict and json with pandas dataframe data使用 pandas 数据框数据填充 dict 和 json
【发布时间】:2021-07-17 20:57:21
【问题描述】:

我需要帮助使用如下所示的 pandas 数据框填充 json:

   vacantes = pd.DataFrame([{'reg': 'Apodaca',
      'puesto': 'Agente telefónico',
      'info': 'izzi estamos buscando personas con vocación en el #Servicio al #Cliente que pueda ayudarnos a brindarle una excelente experiencia a nuestros usuarios, como Ejecutivo Telefónico. ¿Tienes el bachillerato trunco o concluido, facilidad de palabra y resuelves problemas con facilidad? ¡Aplica ahora a nuestras vacantes, envía un inbox o inicia tu proceso en la siguiente liga ,
      'imagen': 'https://dl.airtable.com/.attachments/5a3001522e0ca8a2e89cb8a15867f25d/96dbae57/130195739_816649878896716_1369158596607266717_o.png',
      'distancia': 0},
     {'reg': 'Apodaca',
      'puesto': 'Ejecutivo telemarketing',
      'info': 'Inizzia a trabajar con nosotros en Apodaca. Buscamos personas que sepan negociar, atender clientes, busqué oportunidades constantemente, demuestren habilidades de negociación y facilidad de palabra.Si cuentas con el bachillerato trunco o terminado y eres mayor de edad, inicia tu proceso en esta url',
      'imagen': 'https://dl.airtable.com/.attachments/67e9dfb4181719a1a767b8ac3ad33359/0f02d51d/izzi2.png',
      'distancia': 0}])

我需要的输出如下所示:

payload = {
    "attachment": {
        "type": "template",
        "payload": {
            "template_type": "generic",
            "elements": elements
        }
    }
}

使用 pandas 数据框填充元素的位置:

elements= [
                {
                    "title": ,
                    "image_url": ,
                    "subtitle": ,
                    "buttons": [
                        
                    ]
                }
            ]

我需要 title 为 vacantes['puesto'] 列,subtitle 为 vacantes['info'] 列,image_url 为 vacantes['image'] 列。所以对于这个例子,我需要元素列表中的 2 个项目。请帮忙,谢谢。

【问题讨论】:

    标签: python json pandas dictionary parsing


    【解决方案1】:

    你可以试试这个:

    # Use list() to get column values
    elements = [
        {
            "title": list(vacantes["puesto"]),
            "image_url": list(vacantes["imagen"]),
            "subtitle": list(vacantes["info"]),
            "buttons": [],
        }
    ]
    
    # Build payload
    payload = {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": elements
            }
        }
    }
    
    from pprint import pprint
    pprint(payload)
    # Outputs
    # {'attachment': {'payload': {'elements': [{'buttons': [],
    #                                         'image_url':['https://dl.airtable.com/..._o.png',
    #                                         'https://dl.airtable.com/.../zzi2.png'],
    #                                         'subtitle': ['izzi estamos buscando '
    #                                                      'personas con vocación '
    #                                                      'en el #Servicio al ' ...],
    #                                         'title': ['Agente telefónico',
    #                                                   'Ejecutivo '
    #                                                   'telemarketing']}],
    #                           'template_type': 'generic'},
    #               'type': 'template'}}
    

    【讨论】:

    • 谢谢,但我并没有得到预期的值,例如:“title”:list(vacantes["puesto"]) gets me title": ['Agente telefónico', 'Ejecutivo telemarketing' ] 我需要的是在元素中为空缺的每个值重复整个字典
    • 如果 ['Agente telefónico', 'Ejecutivo telemarketing'] 在主字典中拆分,每个元素的键名是什么,知道它们不能重复吗?在您的帖子中添加完整预期结果的示例将有助于为您提供适当的帮助。
    猜你喜欢
    • 2022-06-13
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2019-01-29
    • 2017-04-03
    • 2023-04-08
    • 2022-01-17
    相关资源
    最近更新 更多