Correggere i dtypes e le righe duplicate
Convertire i tipi ed eliminare i duplicati.
Correggere i dtypes e le righe duplicate è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Clean Goes Beyond Gaps
Filling missing values is only half the job. Clean data also needs correct dtypes and no accidental duplicate rows muddying your counts. 🧹
What a dtype Is
Every column has a dtype, its data type, like int, float, object, or datetime. The dtype controls which operations the column even allows.
df.dtypesNumbers Stuck as Text
A frequent mess: numbers loaded as object strings because one stray symbol crept in. You cannot do math on them until the type is fixed.
Coerce With to_numeric
Convert a text column to numbers with to_numeric. Pass errors='coerce' to turn anything unparseable into NaN instead of crashing.
df['price'] = pd.to_numeric(df['price'], errors='coerce')Cast With astype
When a column is already clean, astype changes its type directly, for example turning whole-number floats back into compact integers.
df['count'] = df['count'].astype(int)Fix Date Columns
Dates often arrive as text. Convert them with to_datetime so you can sort, filter by range, and extract parts like month later.
df['date'] = pd.to_datetime(df['date'])Category to Save Memory
Columns with few repeated text values shrink dramatically as the category dtype, which stores each label once and references it.
df['city'] = df['city'].astype('category')Spotting Duplicate Rows
Repeated records inflate totals and averages. Find them with duplicated(), which flags each row that has appeared before as True.
df.duplicated().sum()Dropping Duplicates
Remove repeats with drop_duplicates(). By default it keeps the first occurrence of each row and discards the rest.
df = df.drop_duplicates()Duplicates by Key Columns
Sometimes only certain columns define a duplicate. Pass subset to detect repeats based on a key like an id, ignoring the other columns.
df.drop_duplicates(subset=['user_id'])Verify Before You Move On
After fixing types and repeats, run info() once more. Confirming dtypes and row counts protects you from cleaning errors slipping downstream.
df.info()Quick Check
A price column loaded as object text. Which call safely turns bad entries into NaN?
Recap: Types and Dupes Tamed
You now fix dtypes with to_numeric, astype, and to_datetime, and clear repeats with drop_duplicates. Your table is finally analysis-ready.
Domande Frequenti
La lezione «Correggere i dtypes e le righe duplicate» è gratuita?
Sì — il testo completo di «Correggere i dtypes e le righe duplicate» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.
Cosa imparerò in «Correggere i dtypes e le righe duplicate»?
Convertire i tipi ed eliminare i duplicati. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Data Science Academy?
Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Correggere i dtypes e le righe duplicate»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Data Science Academy?
Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Trovare i NaN nascosti nella tabella
- Eliminare o riempire: scegliere con criterio
- Imputare con media, mediana o moda
- Correggere i dtypes e le righe duplicate