Use the DataFrame.from_pandas() function to create a teradataml DataFrame using a pandas DataFrame.
Required Parameter
- pandas_df
- Specifies the pandas DataFrame to be converted to teradataml DataFrame.
Optional Parameters
- index
- Specifies whether to save Pandas DataFrame index as a column or not.
Default value = True
- index_label
- Specifies the column labels for Pandas DataFrame index columns.Refer to the 'index_label' parameter of copy_to_sql() for more details.
- primary_index
- Specifies which columns to use as primary index for the teradataml DataFrame.
Example setup
>>> import pandas as pd
>>> from teradataml import DataFrame
>>> pdf = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})
>>> pdf1 = pd.DataFrame([[1, 2], [3, 4]])
Example 1: Create a teradataml DataFrame from a pandas DataFrame
>>> df = DataFrame.from_pandas(pdf) >>> df
col1 col2 index_label 0 3 6 2 1 2 5 1 2 1 4 0
Example 2: Create a teradataml DataFrame from a pandas DataFrame and do not save the index as a column
>>> df = DataFrame.from_pandas(pdf, index=False) >>> df
col1 col2 0 3 6 1 2 5 2 1 4
Example 3: Create a teradataml DataFrame from a pandas DataFrame with index label as 'id' and set it as primary index
>>> df = DataFrame.from_pandas(pdf, index=True, index_label='id', primary_index='id') >>> df
col1 col2 id 2 3 6 1 2 5 0 1 4
Example 4: Create a teradataml DataFrame from a pandas DataFrame where columns are not explicitly defined in the pandas DataFrame
>>> df = DataFrame.from_pandas(pdf1) >>> df
col_0 col_1 index_label 0 3 4 1 1 1 2 0