DataFrame() or DataFrame.from_table() Function

Teradata® Python Package User Guide

brand
Teradata Vantage
prodname
Teradata Python Package
vrm_release
16.20
category
User Guide
featnum
B700-4006-098K

The DataFrame() and DataFrame.from_table() functions have identical functionality. Each function constructs a teradataml DataFrame from an existing Teradata Vantage table or view.

The function takes a table name or view name as argument and creates a DataFrame based on the table or view in Teradata.

The function also takes an index label as an optional argument. The index label is used for sorting. The value of the index label can be a column name or a list of column names.
  • If the index label is not specified for a table, the primary index of the base table will be used as the index label.
  • If the index label is not specified for a view, the index label is set to None.
For the following examples, output row orders may vary. To sort the DataFrame rows, use the DataFrame sort() Method.

Example: DataFrame with Default Index Label

This example creates the DataFrame "df" from the existing Teradata table "sales". Because the command omits the index_label argument, the index label is the primary index of "sales", which is "accounts".

>>> df = DataFrame("sales")

or

>>> df = DataFrame.from_table("sales")
>>> df
              Feb    Jan    Mar    Apr    datetime
accounts
Alpha Co    210.0  200.0  215.0  250.0  2017-04-01
Orange Inc  210.0    NaN    NaN  250.0  2017-04-01
Red Inc     200.0  150.0  140.0    NaN  2017-04-01
Jones LLC   200.0  150.0  140.0  180.0  2017-04-01
Yellow Inc   90.0    NaN    NaN    NaN  2017-04-01
Blue Inc     90.0   50.0   95.0  101.0  2017-04-01

Example: DataFrame with One-Column Index Label

This example creates the DataFrame "df" from the existing Teradata table "sales" with an index_label composed of one column, "Feb".

>>> df = DataFrame("sales", index_label="Feb")

or

>>> df = DataFrame.from_table("sales", index_label="Feb")
>>> df
         accounts    Jan    Mar    Apr    datetime
Feb
200.0   Jones LLC  150.0  140.0  180.0  2017-04-01
90.0     Blue Inc   50.0   95.0  101.0  2017-04-01
90.0   Yellow Inc    NaN    NaN    NaN  2017-04-01
200.0     Red Inc  150.0  140.0    NaN  2017-04-01
210.0    Alpha Co  200.0  215.0  250.0  2017-04-01
210.0  Orange Inc    NaN    NaN  250.0  2017-04-01

Example: DataFrame with Two-Column Index Label

This example creates the DataFrame "df" from the existing Teradata table "sales" with an index_label composed of two columns, "Jan" and "Feb".

>>> df = DataFrame("sales", index_label=["Jan", "Feb"])
>>> df
               accounts    Mar    Apr    datetime
Jan   Feb
NaN   90.0   Yellow Inc    NaN    NaN  2017-04-01
150.0 200.0   Jones LLC  140.0  180.0  2017-04-01
NaN   210.0  Orange Inc    NaN  250.0  2017-04-01
200.0 210.0    Alpha Co  215.0  250.0  2017-04-01
50.0  90.0     Blue Inc   95.0  101.0  2017-04-01
150.0 200.0     Red Inc  140.0    NaN  2017-04-01

Example: DataFrame from a Teradata View

This example creates a DataFrame from an existing Teradata view "salesv" with an index_label "Mar".

>>> df = DataFrame("salesv", index_label="Mar")
>>> df
          accounts    Feb    Jan    Apr    datetime
Mar
NaN     Yellow Inc   90.0    NaN    NaN  2017-04-01
 140.0   Jones LLC  200.0  150.0  180.0  2017-04-01
NaN     Orange Inc  210.0    NaN  250.0  2017-04-01
 215.0    Alpha Co  210.0  200.0  250.0  2017-04-01
 95.0     Blue Inc   90.0   50.0  101.0  2017-04-01
 140.0     Red Inc  200.0  150.0    NaN  2017-04-01