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
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
:
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']]
With a list of dicts, a dataframe is as easy as:
df = pd.DataFrame(records)
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))
{'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
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')