Use the DataFrame.from_records() function to create a DataFrame from a list of lists/tuples/dictionaries/numpy arrays.
Required Parameter
- data
- Specifies the iterator of data or the list of lists/tuples/dictionaries/numpy arrays to be converted to teradataml DataFrame.Nested lists or tuples or dictionaries are not supported.
Optional Parameters
- columns
- Specifies the column names for the DataFrame.If the data is a list of lists/tuples/numpy arrays and this argument is not specified, column names will be auto-generated as 'col_0', 'col_1', etc.
- kwargs
- Specifies the columns to be excluded from the DataFrame.
- coerce_float
- Specifies whether to convert values of non-string, non-numeric objects (like decimal.Decimal) to floating point, useful for SQL result sets.
- nrows
- Specifies the number of rows to be read from the data if the data is iterator.
- persist
- Specifies whether to persist the DataFrame.
Default value: false
Example setup
>>> from teradataml import DataFrame
Example 1: Create a teradataml DataFrame from a list of lists
>>> df = DataFrame.from_records([['Alice', 1], ['Bob', 2]], columns=['name', 'age']) >>> df
name age 0 Bob 2 1 Alice 1
Example 2: Create a teradataml DataFrame from a list of tuples
>>> df = DataFrame.from_records([('Alice', 1), ('Bob', 3)], columns=['name', 'age'])
>>> df
name age 0 Bob 3 1 Alice 1
Example 3: Create a teradataml DataFrame from a list of dictionaries
>>> df = DataFrame.from_records([{'name': 'Alice', 'age': 4}, {'name': 'Bob', 'age': 2}])
>>> df
name age 0 Bob 2 1 Alice 4
Example 4: Create a teradataml DataFrame from a list where columns are not explicitly defined
>>> df = DataFrame.from_records([['Alice', 1], ['Bob', 2]]) >>> df
col_0 col_1 0 Bob 2 1 Alice 1
Example 5: Create a teradataml DataFrame from a list by excluding 'grade' column
>>> df = DataFrame.from_records([['Alice', 1, 'A'], ['Bob', 2, 'B']], ... columns=['name', 'age', 'grade'], ... exclude=['grade']) >>> df
name age 0 Bob 2 1 Alice 1
Example 6: Create a teradataml DataFrame from a list of lists with "coerce_float" set to False
>>> df = DataFrame.from_records([[1, Decimal('2.5')], [3, Decimal('4.0')]],
... columns=['col1', 'col2'], coerce_float=False)
>>> df
col1 col2 0 3 4.0 1 1 2.5
>>> df.tdtypes
col1 BIGINT() col2 VARCHAR(length=1024, charset='UNICODE')
Example 7: Create a teradataml DataFrame from a list of lists with "coerce_float" set to True
>>> from decimal import Decimal
>>> df = DataFrame.from_records([[1, Decimal('2.5')], [3, Decimal('4.0')]],
... columns=['col1', 'col2'], coerce_float=True)
>>> df
col1 col2 0 3 4.0 1 1 2.5
>>> df.tdtypes
col1 BIGINT() col2 FLOAT()
Example 8: Create a teradataml DataFrame from an iterator with 'nrows' set to 2
>>> def data_gen(): ... yield ['Alice', 1] ... yield ['Bob', 2] ... yield ['Charlie', 3] >>> df = DataFrame.from_records(data_gen(), columns=['name', 'age'], nrows=2) >>> df
name age 0 Bob 2 1 Alice 1
Example 9: Persist the data from records into a table which can be used across sessions
>>> df = DataFrame.from_records([['Alice', 1], ['Bob', 2]], columns=['name', 'age'], persist=True) >>> df
name age
0 Bob 2
1 Alice 1
Get the database object name.
>>> df.db_object_name
'"ml__from_pandas_1761719973588452"'
Create the teradataml DataFrame using the database object name in different session.
>>> DataFrame('"ml__from_pandas_1761719973588452"')
name age
0 Bob 2
1 Alice 1