Teradata Package for Python Function Reference - set_index - Teradata Package for Python - Look here for syntax, methods and examples for the functions included in the Teradata Package for Python.

Teradata® Package for Python Function Reference

Product
Teradata Package for Python
Release Number
17.00
Published
November 2021
Language
English (United States)
Last Update
2021-11-19
lifecycle
previous
Product Category
Teradata Vantage
teradataml.dataframe.dataframe.DataFrame.set_index = set_index(self, keys, drop=True, append=False)
    DESCRIPTION:
        Assigns one or more existing columns as the new index to a teradataml DataFrame.
 
    PARAMETERS:
 
        keys:
            Required Argument.
            Specifies the column name or a list of column names to use as the DataFrame index.
            Types: str OR list of Strings (str)
 
        drop:
            Optional Argument.
            Specifies whether or not to display the column(s) being set as index as
            teradataml DataFrame columns anymore.
            When drop is True, columns are set as index and not displayed as columns.
            When drop is False, columns are set as index; but also displayed as columns.
            Note: When the drop argument is set to True, the column being set as index does not cease to
                  be a part of the underlying table upon which the teradataml DataFrame is based off.
                  A column that is dropped while being set as an index is merely not used for display
                  purposes anymore as a column of the teradataml DataFrame.
            Default Value: True
            Types: bool
 
        append:
            Optional Argument.
            Specifies whether or not to append requested columns to the existing index.
`           When append is False, replaces existing index.
            When append is True, retains both existing & currently appended index.
            Default Value: False
            Types: bool
 
    RETURNS:
        teradataml DataFrame
 
    RAISES:
        TeradataMlException
 
    EXAMPLES:
        >>> load_example_data("dataframe","admissions_train")
        >>> df = DataFrame("admissions_train")
        >>> df.sort('id')
           masters   gpa     stats programming admitted
        id
        1      yes  3.95  Beginner    Beginner        0
        2      yes  3.76  Beginner    Beginner        0
        3       no  3.70    Novice    Beginner        1
        4      yes  3.50  Beginner      Novice        1
        5       no  3.44    Novice      Novice        0
        6      yes  3.50  Beginner    Advanced        1
        7      yes  2.33    Novice      Novice        1
        8       no  3.60  Beginner    Advanced        1
        9       no  3.82  Advanced    Advanced        1
        10      no  3.71  Advanced    Advanced        1
 
        >>> # Set new index.
        >>> df.set_index('masters').sort('id')
                 id   gpa     stats programming admitted
        masters
        yes       1  3.95  Beginner    Beginner        0
        yes       2  3.76  Beginner    Beginner        0
        no        3  3.70    Novice    Beginner        1
        yes       4  3.50  Beginner      Novice        1
        no        5  3.44    Novice      Novice        0
        yes       6  3.50  Beginner    Advanced        1
        yes       7  2.33    Novice      Novice        1
        no        8  3.60  Beginner    Advanced        1
        no        9  3.82  Advanced    Advanced        1
        no       10  3.71  Advanced    Advanced        1
 
        >>> # Set multiple indexes using list of columns
        >>> df.set_index(['masters', 'id']).sort('id')
                     gpa     stats programming admitted
        id masters
        1  yes      3.95  Beginner    Beginner        0
        2  yes      3.76  Beginner    Beginner        0
        3  no       3.70    Novice    Beginner        1
        4  yes      3.50  Beginner      Novice        1
        5  no       3.44    Novice      Novice        0
        6  yes      3.50  Beginner    Advanced        1
        7  yes      2.33    Novice      Novice        1
        8  no       3.60  Beginner    Advanced        1
        9  no       3.82  Advanced    Advanced        1
        10 no       3.71  Advanced    Advanced        1
 
        >>> # Append to new index to the existing set of index.
        >>> df.set_index(['masters', 'id']).set_index('gpa', drop = False, append = True).sort('id')
                            stats programming admitted
        gpa  masters id
        3.95 yes     1   Beginner    Beginner        0
        3.76 yes     2   Beginner    Beginner        0
        3.70 no      3     Novice    Beginner        1
        3.50 yes     4   Beginner      Novice        1
        3.44 no      5     Novice      Novice        0
        3.50 yes     6   Beginner    Advanced        1
        2.33 yes     7     Novice      Novice        1
        3.60 no      8   Beginner    Advanced        1
        3.82 no      9   Advanced    Advanced        1
        3.71 no      10  Advanced    Advanced        1
        >>>