Pandas DataFrame Supprimer l'index

Suraj Joshi 30 janvier 2023
  1. Supprimer l’index d’un Pandas DataFrame en utilisant la méthode reset_index()
  2. Suppression de l’index d’un Pandas DataFrame en utilisant la méthode set_index()
Pandas DataFrame Supprimer l'index

Ce tutoriel explique comment supprimer l’index de Pandas DataFrame.

Nous utiliserons le DataFrame présenté ci-dessous pour montrer comment nous pouvons supprimer l’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"],
)

print(my_df)

Production :

    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

Supprimer l’index d’un Pandas DataFrame en utilisant la méthode reset_index()

Le pandas.DataFrame.reset_index() réinitialisera l’index de la DataFrame à l’index par défaut.

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)

Production :

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

Il va réinitialiser l’index de la DataFrame my_df mais l’index apparaîtra maintenant comme la colonne index. Si nous voulons supprimer la colonne index, nous pouvons mettre drop=True dans la méthode 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)

Production :

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

Suppression de l’index d’un Pandas DataFrame en utilisant la méthode set_index()

Le pandas.DataFrame.set_index() définira la colonne passée en argument comme l’index de la DataFrame en surchargeant l’index initial.

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)

Production :

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

Il définit la colonne Person comme un index de la my_df DataFrame, remplaçant l’index initial de la DataFrame.

Auteur: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

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

LinkedIn

Article connexe - Pandas Index