parent-child relationship query in simple_salesforce python, extracting from ordered dicts

后端 未结 2 1506
后悔当初
后悔当初 2021-01-05 18:11

I\'m trying to query information from salesforce using the simple_salesforce package in python.

The problem is that it\'s nesting fields that are a par

相关标签:
2条回答
  • 2021-01-05 18:22

    Pandas is an amazing tool for tabular data. But while it can contain Python objects, that is not its sweet spot. I suggest you extract your data from the query prior to inserting them into a pandas.Dataframe:

    Extract records:

    To extract the desired fields as a list of dictionaries is as easy as:

    records = [dict(id=rec['Id'], country=rec['Account']['BillingCountry'])
               for rec in data['records']]
    

    Insert records into a dataframe:

    With a list of dicts, a dataframe is as easy as:

    df = pd.DataFrame(records)
    

    Test Code:

    import pandas as pd
    from collections import OrderedDict
    
    data = OrderedDict([
        ('totalSize', 455),
        ('done', True),
        ('records', [
            OrderedDict([
                ('attributes', OrderedDict([('type', 'Opportunity'), ('url', '/services/data/v34.0/sobjects/Opportunity/0061B003451RhZgiHHF')])),
                ('Id', '0061B003451RhZgiHHF'),
                ('Account', OrderedDict([('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v34.0/sobjects/Account/0013000000MvkRQQAZ')])),
                                         ('BillingCountry', 'United States')])),
            ]),
            OrderedDict([
                ('attributes', OrderedDict([('type', 'Opportunity'), ('url', '/services/data/v34.0/sobjects/Opportunity/0061B00001Pa52QQAR')])),
                ('Id', '0061B00001Pa52QQAR'),
                ('Account', OrderedDict([('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v34.0/sobjects/Account/0011300001vQPxqAAG')])),
                                         ('BillingCountry', 'United States')])),
            ]),
            OrderedDict([
                ('attributes', OrderedDict([('type', 'Opportunity'), ('url', '/services/data/v34.0/sobjects/Opportunity/0061B00001TRu5mQAD')])),
                ('Id', '0061B00001TRu5mQAD'),
                ('Account', OrderedDict([('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v34.0/sobjects/Account/0011300001rfRTrAAE')])),
                                         ('BillingCountry', 'United States')])),
            ]),
        ])
    ])
    
    records = [dict(id=rec['Id'], country=rec['Account']['BillingCountry'])
               for rec in data['records']]
    for r in records:
        print(r)
    
    print(pd.DataFrame(records))
    

    Test Results:

    {'country': 'United States', 'id': '0061B003451RhZgiHHF'}
    {'country': 'United States', 'id': '0061B00001Pa52QQAR'}
    {'country': 'United States', 'id': '0061B00001TRu5mQAD'}
    
             country                   id
    0  United States  0061B003451RhZgiHHF
    1  United States   0061B00001Pa52QQAR
    2  United States   0061B00001TRu5mQAD
    
    0 讨论(0)
  • 2021-01-05 18:24

    Pandas can read ordered dicts.

    import pandas as pd
    from simple_salesforce import Salesforce
    
    sf = Salesforce(username='your_username',   
                    password='your_password',
                    security_token='your_token')
    
    query = "select id, account.id from opportunity where closedate = last_n_days:5"
    df = pd.DataFrame(sf.query_all(query)['records']).drop(columns='attributes')
    
    0 讨论(0)
提交回复
热议问题