Pandas DataFrame Remover Índice

Suraj Joshi 30 janeiro 2023
  1. Remover índice de um DataFrame Pandas Utilizando o reset_index() Método
  2. Remover índice de um Pandas DataFrame Utilizando o método set_index()
Pandas DataFrame Remover Índice

Este tutorial explicará como podemos remover o índice de Pandas DataFrame.

Utilizaremos o DataFrame apresentado abaixo para mostrar como podemos remover o índice.

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

print(my_df)

Resultado:

    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

Remover índice de um DataFrame Pandas Utilizando o reset_index() Método

O pandas.DataFrame.reset_index() irá repor o índice do DataFrame para o índice padrão.

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

df_reset = my_df.reset_index()

print("Before reseting Index:")
print(my_df, "\n")

print("After reseting Index:")
print(df_reset)

Resultado:

Before reseting Index:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After reseting Index:
  index   Person      City Mother Tongue  Age
0     A    Alice    Berlin         German   37
1     B   Steven  Montreal         French   20
2     C  Neesham   Toronto        English   38
3     D    Chris      Rome        Italian   23
4     E    Alice    Munich         German   35

irá repor o índice do my_df DataFrame mas o índice irá agora aparecer como a coluna index. Se quisermos largar a coluna index, podemos definir drop=True no método reset_index().

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

df_reset = my_df.reset_index(drop=True)

print("Before reseting Index:")
print(my_df, "\n")

print("After reseting Index:")
print(df_reset)

Resultado:

Before reseting Index:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After reseting Index:
    Person      City Mother Tongue  Age
0    Alice    Berlin         German   37
1   Steven  Montreal         French   20
2  Neesham   Toronto        English   38
3    Chris      Rome        Italian   23
4    Alice    Munich         German   35

Remover índice de um Pandas DataFrame Utilizando o método set_index()

O pandas.DataFrame.set_index() irá definir a coluna passada como um argumento como o índice da DataFrame sobrepondo-se ao índice inicial.

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

df_reset = my_df.set_index("Person")

print("Initial DataFrame:")
print(my_df, "\n")

print("After setting Person column as Index:")
print(df_reset)

Resultado:

Initial DataFrame:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After setting Person column as Index:
             City Mother Tongue  Age
Person
Alice      Berlin         German   37
Steven   Montreal         French   20
Neesham   Toronto        English   38
Chris        Rome        Italian   23
Alice      Munich         German   35

Define a coluna Person como um índice do my_df DataFrame substituindo o índice inicial do DataFrame.

Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Artigo relacionado - Pandas Index