_set_index

pyhelpers.store._set_index(data, index_col=None)[source]

Sets the index of a dataframe using column names or integer positions.

Parameters:
  • data (pandas.DataFrame) – The dataframe to update.

  • index_col (str | int | list | None) – Column name(s) or integer index/indices to set as the index. If None (default), the function sets the first column as the index only if its name is an empty string ('') or starts with 'Unnamed:'.

Returns:

The dataframe with the updated index.

Return type:

pandas.DataFrame

Tests:

>>> from pyhelpers.store import _set_index
>>> from pyhelpers._cache import example_dataframe
>>> import numpy as np
>>> example_df = example_dataframe()
>>> example_df
            Longitude   Latitude
City
London      -0.127647  51.507322
Birmingham  -1.902691  52.479699
Manchester  -2.245115  53.479489
Leeds       -1.543794  53.797418
>>> example_df.equals(_set_index(example_df))
True
>>> example_df_1 = _set_index(example_df, index_col=0)
>>> example_df_1
            Latitude
Longitude
-0.127647  51.507322
-1.902691  52.479699
-2.245115  53.479489
-1.543794  53.797418
>>> example_df.iloc[:, 0].to_list() == example_df_1.index.to_list()
True
>>> example_df_2 = example_df.copy()
>>> example_df_2.index.name = ''
>>> example_df_2.reset_index(inplace=True)
>>> example_df_2 = _set_index(example_df_2, index_col=None)
>>> np.array_equal(example_df_2.values, example_df.values)
True