HOWTO · Pandas
Pandas Drop Rows
Questo tutorial spiega come possiamo rilasciare righe in Pandas usando il metodo pandas.DataFrame.drop().
In questa pagina
Questo tutorial spiega come possiamo rilasciare righe in Pandas usando il metodo pandas.DataFrame.drop().
import pandas as pd
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [30, 33, 35, 30, 30],
"Weight(KG)": [75, 75, 80, 70, 73],
}
)
print("The KGP DataFrame is:")
print(kgp_df)
Produzione:
The KGP DataFrame is:
Name Age Weight(KG)
0 Himansh 30 75
1 Prateek 33 75
2 Abhishek 35 80
3 Vidit 30 70
4 Anupam 30 73
Useremo il kgp_df DataFrame per spiegare come possiamo rilasciare righe dal Pandas DataFrame.
Trascina le righe dall’indice nel metodo pandas.DataFrame.drop()
import pandas as pd
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [30, 33, 35, 30, 30],
"Weight(KG)": [75, 75, 80, 70, 73],
}
)
rows_dropped_df = kgp_df.drop(kgp_df.index[[0, 2]])
print("The KGP DataFrame is:")
print(kgp_df, "\n")
print("The KGP DataFrame after dropping 1st and 3rd DataFrame is:")
print(rows_dropped_df)
Produzione:
The KGP DataFrame is:
Name Age Weight(KG)
0 Himansh 30 75
1 Prateek 33 75
2 Abhishek 35 80
3 Vidit 30 70
4 Anupam 30 73
The KGP DataFrame after dropping 1st and 3rd DataFrame is:
Name Age Weight(KG)
1 Prateek 33 75
3 Vidit 30 70
4 Anupam 30 73
Elimina le righe con gli indici 0 e 2 dal DataFrame kgp_df. Le righe con gli indici 0 e 2 corrispondono alla prima e alla terza riga nel DataFrame perché l’indice inizia da 0.
Possiamo anche usare l’indice del DataFrame per eliminare le righe invece di usare l’indice predefinito.
import pandas as pd
kgp_idx = ["A", "B", "C", "D", "E"]
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [30, 33, 35, 30, 30],
"Weight(KG)": [75, 75, 80, 70, 73],
},
index=kgp_idx,
)
rows_dropped_df = kgp_df.drop(["A", "C"])
print("The KGP DataFrame is:")
print(kgp_df, "\n")
print("The KGP DataFrame after dropping 1st and 3rd DataFrame is:")
print(rows_dropped_df)
Produzione:
The KGP DataFrame is:
Name Age Weight(KG)
A Himansh 30 75
B Prateek 33 75
C Abhishek 35 80
D Vidit 30 70
E Anupam 30 73
The KGP DataFrame after dropping 1st and 3rd DataFrame is:
Name Age Weight(KG)
B Prateek 33 75
D Vidit 30 70
E Anupam 30 73
Elimina le righe con indice A e C o la prima e la terza riga dal DataFrame.
Passiamo l’lista degli indici delle righe da rilasciare al metodo drop() per eliminare le rispettive righe.
Rilascia le righe a seconda del valore di una particolare colonna in Pandas DataFrame
import pandas as pd
kgp_idx = ["A", "B", "C", "D", "E"]
kgp_df = pd.DataFrame(
{
"Name": ["Himansh", "Prateek", "Abhishek", "Vidit", "Anupam"],
"Age": [31, 33, 35, 36, 34],
"Weight(KG)": [75, 75, 80, 70, 73],
},
index=kgp_idx,
)
young_df_idx = kgp_df[kgp_df["Age"] <= 33].index
young_folks = kgp_df.drop(young_df_idx)
print("The KGP DataFrame is:")
print(kgp_df, "\n")
print("The DataFrame of folks with age less than or equal to 33 are:")
print(young_folks)
Produzione:
The KGP DataFrame is:
Name Age Weight(KG)
A Himansh 31 75
B Prateek 33 75
C Abhishek 35 80
D Vidit 36 70
E Anupam 34 73
The DataFrame of folks with age less than or equal to 33 are:
Name Age Weight(KG)
C Abhishek 35 80
D Vidit 36 70
E Anupam 34 73
Eliminerà tutte le righe con età inferiore o uguale a 33.
Per prima cosa troviamo l’indice di tutte le righe con età inferiore o uguale a 33 e poi rilasciamo le righe utilizzando il metodo drop().