_set_index¶
- pyhelpers.store._set_index(data, index=None)[source]¶
Sets the index of a dataframe.
- Parameters:
data (pandas.DataFrame) – The dataframe to update.
index (int | list | None) – Column index or a list of column indices to set as the index; when
index=None
(default), the function sets the first column as the index if its column name is an empty string.
- 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=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=None) >>> np.array_equal(example_df_2.values, example_df.values) True