Python PyTd teradata Query Into Pandas DataFrame

假装没事ソ 提交于 2019-12-04 21:20:03

You can use pandas.read_sql :)

import teradata
import pandas as pd

# teradata connection
udaExec = teradata.UdaExec(appName="Example", version="1.0",
                           logConsole=False)
with udaExec.connect(method="odbc", system="", username="", password="") as session:


    query ="SELECT * FROM table"

    df = pd.read_sql(query,session)

Using ‘with’ will ensure close of session after the query. I hope that helped :)

I know its a little late. But putting a note nevertheless.

There are a few questions here.

How can I read my data into a dataframe from Teradata using the teradata module?

At the end of the day, a teradata.util.Row is simply a list. So a simple list operation should help you get things out of Row.

','.join(str(item) for item in row)

kinda thing.

Pushing that into a pandas dataframe should be a list to df conversion exercise.

I'm not able to use the pyodbc module for this.

I used teradata's python module to do a LDAP auth. All worked fine. Didn't have this requirement. Sorry.

Is there a better way to create the empty dataframe with column names matching those in the database?

I assume, given a table name, you can query to figure it schema (table names) >> convert that to a list and create your pandas df?

I know this is very late.

You can use read_sql() from pandas module. It returns pandas dataframe.

Here is the reference: http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.read_sql.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!