How to fetch the data from Teradata using Python pandas
Contents
Teradata module for Python
Teradata module enables Python applications to connect to the Teradata Database, and interoperates with Teradata Database 12.0 and later releases. It can use either Teradata ODBC or REST API for Teradata database to connect and interact with Teradata.
Python pandas – Dataframe
Pandas is a package/library in python that used for data analysis.It makes importing, analyzing, and visualizing data much easier. Its key data structure is called the DataFrame. A Data frame is a two-dimensional data structure. It allow you to store and manipulate tabular data in rows and columns.
Import the required packages in Python
Lets install the teradata and pandas library in our machine using pip command as below.
1 2 |
pip install teradata pip install pandas |
Python program to fetch the data from Teradata table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import pandas as pd; import teradata; #Establish the connection to the Teradata database udaExec = teradata.UdaExec(appName="test", version="1.0", logConsole=False) connection = udaExec.connect(method="odbc", system="TDPROD", driver="Teradata", username='USERNAME',password='PASSWORD'); #query to fetch the data from the employee table in Teradata query ="SELECT * FROM Banking_DB.Employee"; #Fetch the data from Teradata using Pandas Dataframe pda = pd.read_sql(query,connection) counter = len(pda) #print the rows in the table print("Number of rows:",counter) print(pda) |
The first line is imports the Teradata and pandas library that is used to fetch/store the data from the Teradata database. UdaExec is a framework that handles the configuration and logging the Teradata application. Since we mentioned the
logConsole=False , it will not log to the console so that our print statement is easier to read.
Once the Teradata connection established, we can run the select query to fetch the data from the Teradata database. Here we used pandas as pd that stores the result set of the SELECT query in dataframe.
Finally we just print the result of the query in console.
Changes required from your end before run this program
TDPROD – Modify your Teradata server address
USERNAME – Substitute Teradata user name
PASSWORD – Substitute Teradata password
Database and Table name – Give the valid database and Table name
Output
Recommended Articles