Pandas DataFrame Azzera indice

Suraj Joshi 30 gennaio 2023 Pandas Pandas Index
  1. Pandas DataFrame Metodo reset_index()
  2. Reimpostare l’indice di un DataFrame utilizzando il metodo pandas.DataFrame.reset_index()
Pandas DataFrame Azzera indice

Questo tutorial spiega come possiamo resettare l’indice in Pandas DataFrame usando pandas.DataFrame.reset_index(). Il metodo reset_index() imposta l’indice del DataFrame sull’indice predefinito con numeri che vanno da 0 a (numero di righe in DataFrame-1).

ADVERTISEMENT

Pandas DataFrame Metodo reset_index()

Sintassi

Python
 pythonCopyDataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill="")

Reimpostare l’indice di un DataFrame utilizzando il metodo pandas.DataFrame.reset_index()

Python
 pythonCopyimport pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    },
    index=roll_no,
)

print(student_df)

Produzione:

 textCopy        Name  Age      City Grade
501    Alice   17  New York     A
502   Steven   20  Portland    B-
503  Neesham   18    Boston    B+
504    Chris   21   Seattle    A-
505    Alice   15    Austin     A

Supponiamo di avere un DataFrame con cinque righe e quattro colonne come visualizzato nell’output. Abbiamo anche un indice impostato nel DataFrame.

Reimpostare l’indice di un DataFrame Mantenendo l’indice iniziale di DataFrame come una colonna

Python
 pythonCopyimport pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    },
    index=roll_no,
)

print("Initial DataFrame:")
print(student_df)
print("")

print("DataFrame after reset_index:")
student_df.reset_index(inplace=True, drop=False)
print(student_df)

Produzione:

 textCopyInitial DataFrame:
        Name  Age      City Grade
501    Alice   17  New York     A
502   Steven   20  Portland    B-
503  Neesham   18    Boston    B+
504    Chris   21   Seattle    A-
505    Alice   15    Austin     A

DataFrame after reset_index:
   index     Name  Age      City Grade
0    501    Alice   17  New York     A
1    502   Steven   20  Portland    B-
2    503  Neesham   18    Boston    B+
3    504    Chris   21   Seattle    A-
4    505    Alice   15    Austin     A

Reimposta l’indice del DataFrame student_df sull’indice predefinito. inplace=True apporta la modifica nel DataFrame originale stesso. Se usiamo drop=False, l’indice iniziale viene posizionato come una colonna nel DataFrame dopo aver utilizzato il metodo reset_index().

Reimposta l’indice di un DataFrame Rimozione dell’indice iniziale di DataFrame

Python
 pythonCopyimport pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    },
    index=roll_no,
)

print("Initial DataFrame:")
print(student_df)
print("")

print("DataFrame after reset_index:")
student_df.reset_index(inplace=True, drop=True)
print(student_df)

Produzione:

 textCopyInitial DataFrame:
        Name  Age      City Grade
501    Alice   17  New York     A
502   Steven   20  Portland    B-
503  Neesham   18    Boston    B+
504    Chris   21   Seattle    A-
505    Alice   15    Austin     A

DataFrame after reset_index:
      Name  Age      City Grade
0    Alice   17  New York     A
1   Steven   20  Portland    B-
2  Neesham   18    Boston    B+
3    Chris   21   Seattle    A-
4    Alice   15    Austin     A

Reimposta l’indice del DataFrame student_df sull’indice predefinito. Poiché abbiamo impostato drop=True nel metodo reset_index(), l’indice iniziale viene eliminato dal DataFrame.

Reimposta l’indice di un DataFrame dopo l’eliminazione delle righe

Python
 pythonCopyimport pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    }
)

student_df.drop([2, 3], inplace=True)
print("Initial DataFrame:")
print(student_df)
print("")

student_df.reset_index(inplace=True, drop=True)
print("DataFrame after reset_index:")
print(student_df)

Produzione:

 textCopyInitial DataFrame:
     Name  Age      City Grade
0   Alice   17  New York     A
1  Steven   20  Portland    B-
4   Alice   15    Austin     A

DataFrame after reset_index:
     Name  Age      City Grade
0   Alice   17  New York     A
1  Steven   20  Portland    B-
2   Alice   15    Austin     A

Come possiamo vedere nell’output, mancano gli indici dopo aver eliminato le righe. In questi casi, possiamo usare il metodo reset_index() per usare l’indice senza valori mancanti.

Se vogliamo che l’indice iniziale sia posizionato come colonna del DataFrame, possiamo usare drop=False nel metodo reset_index().

Ti piacciono i nostri tutorial? Iscriviti a DelftStack su YouTube per aiutarci a creare altre guide video di alta qualità. Iscriviti
Autore: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

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

LinkedIn

Articolo correlato - Pandas Index