Pulizia dei dati in Python
Adel Nehme
VP of AI Curriculum, DataCamp
Tutte le colonne hanno gli stessi valori
| first_name | last_name | address | height | weight |
|---|---|---|---|---|
| Justin | Saddlemyer | Boulevard du Jardin Botanique 3, Bruxelles | 193 cm | 87 kg |
| Justin | Saddlemyer | Boulevard du Jardin Botanique 3, Bruxelles | 193 cm | 87 kg |
La maggior parte delle colonne ha gli stessi valori
| first_name | last_name | address | height | weight |
|---|---|---|---|---|
| Justin | Saddlemyer | Boulevard du Jardin Botanique 3, Bruxelles | 193 cm | 87 kg |
| Justin | Saddlemyer | Boulevard du Jardin Botanique 3, Bruxelles | 194 cm | 87 kg |



# Stampa l'intestazione
height_weight.head()
first_name last_name address height weight
0 Lane Reese 534-1559 Nam St. 181 64
1 Ivor Pierce 102-3364 Non Road 168 66
2 Roary Gibson P.O. Box 344, 7785 Nisi Ave 191 99
3 Shannon Little 691-2550 Consectetuer Street 185 65
4 Abdul Fry 4565 Risus St. 169 65
# Ottieni i duplicati su tutte le colonne
duplicates = height_weight.duplicated()
print(duplicates)
1 False
... ....
22 True
23 False
... ...
# Ottieni le righe duplicate
duplicates = height_weight.duplicated()
height_weight[duplicates]
first_name last_name address height weight
100 Mary Colon 4674 Ut Rd. 179 75
101 Ivor Pierce 102-3364 Non Road 168 88
102 Cole Palmer 8366 At, Street 178 91
103 Desirae Shannon P.O. Box 643, 5251 Consectetuer, Rd. 196 83
Il metodo .duplicated()
subset: Elenco di colonne da controllare per i duplicati.
keep: Se tenere il primo ('first'), l'ultimo ('last') o tutti (False) i duplicati.
# Nomi colonne da controllare per duplicati
column_names = ['first_name','last_name','address']
duplicates = height_weight.duplicated(subset = column_names, keep = False)
# Mostra i valori duplicati
height_weight[duplicates]
first_name last_name address height weight
1 Ivor Pierce 102-3364 Non Road 168 66
22 Cole Palmer 8366 At, Street 178 91
28 Desirae Shannon P.O. Box 643, 5251 Consectetuer, Rd. 195 83
37 Mary Colon 4674 Ut Rd. 179 75
100 Mary Colon 4674 Ut Rd. 179 75
101 Ivor Pierce 102-3364 Non Road 168 88
102 Cole Palmer 8366 At, Street 178 91
103 Desirae Shannon P.O. Box 643, 5251 Consectetuer, Rd. 196 83
# Mostra i valori duplicati
height_weight[duplicates].sort_values(by = 'first_name')
first_name last_name address height weight
22 Cole Palmer 8366 At, Street 178 91
102 Cole Palmer 8366 At, Street 178 91
28 Desirae Shannon P.O. Box 643, 5251 Consectetuer, Rd. 195 83
103 Desirae Shannon P.O. Box 643, 5251 Consectetuer, Rd. 196 83
1 Ivor Pierce 102-3364 Non Road 168 66
101 Ivor Pierce 102-3364 Non Road 168 88
37 Mary Colon 4674 Ut Rd. 179 75
100 Mary Colon 4674 Ut Rd. 179 75
# Mostra i valori duplicati
height_weight[duplicates].sort_values(by = 'first_name')

# Mostra i valori duplicati
height_weight[duplicates].sort_values(by = 'first_name')

# Mostra i valori duplicati
height_weight[duplicates].sort_values(by = 'first_name')

Il metodo .drop_duplicates()
subset: Elenco di colonne da controllare per i duplicati.
keep: Se tenere il primo ('first'), l'ultimo ('last') o tutti (False) i duplicati.
inplace: Elimina le righe duplicate direttamente nel DataFrame senza creare un nuovo oggetto (True).
# Rimuovi i duplicati
height_weight.drop_duplicates(inplace = True)
# Mostra i valori duplicati
column_names = ['first_name','last_name','address']
duplicates = height_weight.duplicated(subset = column_names, keep = False)
height_weight[duplicates].sort_values(by = 'first_name')
first_name last_name address height weight
28 Desirae Shannon P.O. Box 643, 5251 Consectetuer, Rd. 195 83
103 Desirae Shannon P.O. Box 643, 5251 Consectetuer, Rd. 196 83
1 Ivor Pierce 102-3364 Non Road 168 66
101 Ivor Pierce 102-3364 Non Road 168 88
# Mostra i valori duplicati
column_names = ['first_name','last_name','address']
duplicates = height_weight.duplicated(subset = column_names, keep = False)
height_weight[duplicates].sort_values(by = 'first_name')

I metodi .groupby() e .agg()
# Raggruppa per colonne e crea riepiloghi statistici column_names = ['first_name','last_name','address'] summaries = {'height': 'max', 'weight': 'mean'} height_weight = height_weight.groupby(by = column_names).agg(summaries).reset_index()# Verifica che l'aggregazione sia fatta duplicates = height_weight.duplicated(subset = column_names, keep = False) height_weight[duplicates].sort_values(by = 'first_name')
first_name last_name address height weight
Pulizia dei dati in Python