- pandas.DataFrame.reset_index#
- pandas.DataFrame.reset_index#
- Как сбросить индекс в Pandas DataFrame(с примерами)
- Пример 1: сбросить индекс и удалить старый индекс
- Пример 2: сбросить индекс и сохранить старый индекс как столбец
- Дополнительные ресурсы
- Pandas DataFrame reset_index(): сброс индексов
- DataFrame в Pandas
- Что такое функция DataFrame set_index() в Pandas?
pandas.DataFrame.reset_index#
Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more levels.
Parameters level int, str, tuple, or list, default None
Only remove the given levels from the index. Removes all levels by default.
drop bool, default False
Do not try to insert index into dataframe columns. This resets the index to the default integer index.
inplace bool, default False
Whether to modify the DataFrame rather than creating a new one.
col_level int or str, default 0
If the columns have multiple levels, determines which level the labels are inserted into. By default it is inserted into the first level.
col_fill object, default ‘’
If the columns have multiple levels, determines how the other levels are named. If None then the index name is repeated.
allow_duplicates bool, optional, default lib.no_default
Allow duplicate column labels to be created.
Using the given string, rename the DataFrame column which contains the index data. If the DataFrame has a MultiIndex, this has to be a list or tuple with length equal to the number of levels.
DataFrame with the new index or None if inplace=True .
Change to new indices or expand indices.
Change to same indices as other DataFrame.
>>> df = pd.DataFrame([('bird', 389.0), . ('bird', 24.0), . ('mammal', 80.5), . ('mammal', np.nan)], . index=['falcon', 'parrot', 'lion', 'monkey'], . columns=('class', 'max_speed')) >>> df class max_speed falcon bird 389.0 parrot bird 24.0 lion mammal 80.5 monkey mammal NaN
When we reset the index, the old index is added as a column, and a new sequential index is used:
>>> df.reset_index() index class max_speed 0 falcon bird 389.0 1 parrot bird 24.0 2 lion mammal 80.5 3 monkey mammal NaN
We can use the drop parameter to avoid the old index being added as a column:
>>> df.reset_index(drop=True) class max_speed 0 bird 389.0 1 bird 24.0 2 mammal 80.5 3 mammal NaN
You can also use reset_index with MultiIndex .
>>> index = pd.MultiIndex.from_tuples([('bird', 'falcon'), . ('bird', 'parrot'), . ('mammal', 'lion'), . ('mammal', 'monkey')], . names=['class', 'name']) >>> columns = pd.MultiIndex.from_tuples([('speed', 'max'), . ('species', 'type')]) >>> df = pd.DataFrame([(389.0, 'fly'), . (24.0, 'fly'), . (80.5, 'run'), . (np.nan, 'jump')], . index=index, . columns=columns) >>> df speed species max type class name bird falcon 389.0 fly parrot 24.0 fly mammal lion 80.5 run monkey NaN jump
Using the names parameter, choose a name for the index column:
>>> df.reset_index(names=['classes', 'names']) classes names speed species max type 0 bird falcon 389.0 fly 1 bird parrot 24.0 fly 2 mammal lion 80.5 run 3 mammal monkey NaN jump
If the index has multiple levels, we can reset a subset of them:
>>> df.reset_index(level='class') class speed species max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
If we are not dropping the index, by default, it is placed in the top level. We can place it in another level:
>>> df.reset_index(level='class', col_level=1) speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
When the index is inserted under another level, we can specify under which one with the parameter col_fill :
>>> df.reset_index(level='class', col_level=1, col_fill='species') species speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
If we specify a nonexistent level for col_fill , it is created:
>>> df.reset_index(level='class', col_level=1, col_fill='genus') genus speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
pandas.DataFrame.reset_index#
Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more levels.
Parameters : level int, str, tuple, or list, default None
Only remove the given levels from the index. Removes all levels by default.
drop bool, default False
Do not try to insert index into dataframe columns. This resets the index to the default integer index.
inplace bool, default False
Whether to modify the DataFrame rather than creating a new one.
col_level int or str, default 0
If the columns have multiple levels, determines which level the labels are inserted into. By default it is inserted into the first level.
col_fill object, default ‘’
If the columns have multiple levels, determines how the other levels are named. If None then the index name is repeated.
allow_duplicates bool, optional, default lib.no_default
Allow duplicate column labels to be created.
Using the given string, rename the DataFrame column which contains the index data. If the DataFrame has a MultiIndex, this has to be a list or tuple with length equal to the number of levels.
DataFrame with the new index or None if inplace=True .
Change to new indices or expand indices.
Change to same indices as other DataFrame.
>>> df = pd.DataFrame([('bird', 389.0), . ('bird', 24.0), . ('mammal', 80.5), . ('mammal', np.nan)], . index=['falcon', 'parrot', 'lion', 'monkey'], . columns=('class', 'max_speed')) >>> df class max_speed falcon bird 389.0 parrot bird 24.0 lion mammal 80.5 monkey mammal NaN
When we reset the index, the old index is added as a column, and a new sequential index is used:
>>> df.reset_index() index class max_speed 0 falcon bird 389.0 1 parrot bird 24.0 2 lion mammal 80.5 3 monkey mammal NaN
We can use the drop parameter to avoid the old index being added as a column:
>>> df.reset_index(drop=True) class max_speed 0 bird 389.0 1 bird 24.0 2 mammal 80.5 3 mammal NaN
You can also use reset_index with MultiIndex .
>>> index = pd.MultiIndex.from_tuples([('bird', 'falcon'), . ('bird', 'parrot'), . ('mammal', 'lion'), . ('mammal', 'monkey')], . names=['class', 'name']) >>> columns = pd.MultiIndex.from_tuples([('speed', 'max'), . ('species', 'type')]) >>> df = pd.DataFrame([(389.0, 'fly'), . (24.0, 'fly'), . (80.5, 'run'), . (np.nan, 'jump')], . index=index, . columns=columns) >>> df speed species max type class name bird falcon 389.0 fly parrot 24.0 fly mammal lion 80.5 run monkey NaN jump
Using the names parameter, choose a name for the index column:
>>> df.reset_index(names=['classes', 'names']) classes names speed species max type 0 bird falcon 389.0 fly 1 bird parrot 24.0 fly 2 mammal lion 80.5 run 3 mammal monkey NaN jump
If the index has multiple levels, we can reset a subset of them:
>>> df.reset_index(level='class') class speed species max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
If we are not dropping the index, by default, it is placed in the top level. We can place it in another level:
>>> df.reset_index(level='class', col_level=1) speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
When the index is inserted under another level, we can specify under which one with the parameter col_fill :
>>> df.reset_index(level='class', col_level=1, col_fill='species') species speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
If we specify a nonexistent level for col_fill , it is created:
>>> df.reset_index(level='class', col_level=1, col_fill='genus') genus speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
Как сбросить индекс в Pandas DataFrame(с примерами)
Вы можете использовать следующий синтаксис для сброса индекса в pandas DataFrame:
df.reset_index(drop= True , inplace= True )
Обратите внимание на следующие аргументы:
- drop : Указание True запрещает pandas сохранять исходный индекс в виде столбца в DataFrame.
- inplace : Указание True позволяет pandas заменить индекс в исходном DataFrame вместо создания копии DataFrame.
В следующих примерах показано, как использовать этот синтаксис на практике.
Пример 1: сбросить индекс и удалить старый индекс
Предположим, у нас есть следующие Pandas DataFrame:
import pandas as pd #define DataFrame df = pd.DataFrame(, index=[0, 4, 3, 5, 2, 1, 7, 6]) #view DataFrame print(df) points assists rebounds 0 25 5 11 4 12 7 8 3 15 7 10 5 14 9 6 2 19 12 6 1 23 9 5 7 25 9 9 6 29 4 12
В следующем коде показано, как сбросить индекс DataFrame и полностью удалить старый индекс:
#reset index df.reset_index(drop= True , inplace= True ) #view updated DataFrame print(df) points assists rebounds 0 25 5 11 1 12 7 8 2 15 7 10 3 14 9 6 4 19 12 6 5 23 9 5 6 25 9 9 7 29 4 12
Обратите внимание, что индекс был сброшен, и значения в индексе теперь находятся в диапазоне от 0 до 7.
Пример 2: сбросить индекс и сохранить старый индекс как столбец
Предположим, у нас есть следующие Pandas DataFrame:
import pandas as pd #define DataFrame df = pd.DataFrame(, index=['A', 'C', 'D', 'B', 'E', 'G', 'F', 'H']) #view DataFrame print(df) points assists rebounds A 25 5 11 C 12 7 8 D 15 7 10 B 14 9 6 E 19 12 6 G 23 9 5 F 25 9 9 H 29 4 12
В следующем коде показано, как сбросить индекс DataFrame и сохранить старый индекс в виде столбца в DataFrame:
#reset index and retain old index as a column df.reset_index(inplace= True ) #view updated DataFrame print(df) index points assists rebounds 0 A 25 5 11 1 C 12 7 8 2 D 15 7 10 3 B 14 9 6 4 E 19 12 6 5 G 23 9 5 6 F 25 9 9 7 H 29 4 12
Обратите внимание, что индекс был сброшен, и значения в индексе теперь находятся в диапазоне от 0 до 7.
Также обратите внимание, что старый индекс (с буквами) сохраняется как новый столбец в DataFrame под названием «индекс».
Дополнительные ресурсы
В следующих руководствах объясняется, как выполнять другие распространенные операции в pandas:
Pandas DataFrame reset_index(): сброс индексов
Pandas DataFrame reset_index() используется для сброса индекса DataFrame. Функция reset_index() используется для установки списка целых чисел от 0 до длины данных в качестве индекса.
Метод reset_index() полезен, когда индекс необходимо рассматривать как столбец или когда индекс не имеет смысла и его необходимо сбросить до значения по умолчанию перед другой операцией. В случае MultiIndex метод reset_index() может использоваться для удаления одного или нескольких уровней. Он либо сбрасывает индекс, либо его уровень. Давайте начнем с нескольких основных.
DataFrame в Pandas
DataFrame — это не что иное, как представление листа Excel в памяти с помощью языка программирования Python. Объект index представляет собой неизменяемый массив. Индексация позволяет нам получить доступ к строке или столбцу с помощью метки. Pandas DataFrame — это композиция, содержащая двумерные данные и связанные с ними метки.
DataFrames широко используются в науке о данных, машинном обучении, научных вычислениях и многих других областях, связанных с интенсивным использованием данных.
Существует множество способов создать Pandas DataFrame. В большинстве случаев вы будете использовать конструктор DataFrame и заполнять данные, метки и другую информацию. Иногда вы будете импортировать данные из файла CSV или Excel, можете передать данные в виде двумерного списка, кортежа или массива NumPy. Вы также можете передать его как экземпляр словаря или Pandas Series или как один из многих других типов данных, не рассматриваемых в этом примере.
Прежде чем продолжить, давайте разберемся с set_index() в Pandas.
Что такое функция DataFrame set_index() в Pandas?
Pandas DataFrame set_index() — это функция, которая используется для установки списка, серии или фрейма данных в качестве индекса фрейма данных. Pandas DataFrame — это двухмерная структура данных с метками, столбцы которой потенциально могут быть другого типа.