5
votes

Currently we are taking SalesForce data in to CSV file and reading this CSV file in Pandas using read_csv, to_csv methods. Do we have any other way to get data from SalesForce to pandas dataframe.

2

2 Answers

7
votes

With Python - you can download a package called Simple Salesforce and write SOQL queries to return data

https://github.com/simple-salesforce/simple-salesforce

Here's an example of how to do this:

from simple_salesforce import Salesforce
sf = Salesforce(username='<enter username>', password='<enter password>', 
     security_token = '<enter your access token from your profile>')

a_query= pd.DataFrame(sf.query(
     "SELECT Name, CreatedDate FROM User")['records'])
2
votes

In my case, to display the information as a dataframe I had to use the following code:

# Import libraries
import simple_salesforce as ssf, pandas

# Create the connection
session_id, instance = ssf.SalesforceLogin(username='<username>', password='<password>', security_token='<token>', sandbox=False)
sf_ = ssf.Salesforce(instance=instance, session_id=session_id)

# Query to execute
sql_code = "SELECT id, name FROM main_table"

# Store query result as dataframe
information = sf_.query(query= sql_code)
table = pandas.DataFrame(information['records']).drop(columns='attributes')