bigframes.pandas.DataFrame.set_index#

DataFrame.set_index(keys: Hashable | Sequence[Hashable], append: bool = False, drop: bool = True) DataFrame[source]#

Set the DataFrame index using existing columns.

Set the DataFrame index (row labels) using one existing column. The index can replace the existing index.

Examples:

>>> df = bpd.DataFrame({'month': [1, 4, 7, 10],
...                     'year': [2012, 2014, 2013, 2014],
...                     'sale': [55, 40, 84, 31]})
>>> df
   month  year  sale
0      1  2012    55
1      4  2014    40
2      7  2013    84
3     10  2014    31

[4 rows x 3 columns]

Set the ‘month’ column to become the index:

>>> df.set_index('month')
       year  sale
month
1      2012    55
4      2014    40
7      2013    84
10     2014    31

[4 rows x 2 columns]

Create a MultiIndex using columns ‘year’ and ‘month’:

>>> df.set_index(['year', 'month'])
            sale
year month
2012 1        55
2014 4        40
2013 7        84
2014 10       31

[4 rows x 1 columns]
Parameters:
  • keys – A label. This parameter can be a single column key.

  • drop – Delete columns to be used as the new index.

Returns:

Changed row labels.

Return type:

bigframes.pandas.DataFrame

Raises:

KeyError – If key(s) are not in the columns.