Eseguire il merge su chiavi e indici
Unire per colonne o per indice.
Eseguire il merge su chiavi e indici è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 2 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.
Same Name, One Word
When both tables share a column with the same name, on is all you need to merge them together.
orders.merge(customers, on="customer_id")Join on Many Keys
Sometimes one column is not enough. Pass a list to on and pandas matches rows on every key at once.
sales.merge(targets, on=["region", "month"])Different Names, No Problem
When key columns are named differently, use left_on and right_on to tell pandas which column maps to which.
orders.merge(users, left_on="uid", right_on="id")A Leftover Column
With left_on and right_on, both key columns survive in the result. You often drop the duplicate afterward.
merged.drop(columns="id")The Index Can Be a Key Too
If your key lives in the index instead of a column, set left_index or right_index to True to join on it.
orders.merge(prices, left_on="sku", right_index=True)join for Index Merges
For two index-aligned tables, the join method is shorter. It merges on the index by default.
left.join(right)Handle Overlapping Names
When both tables have a column with the same name, pandas adds suffixes so the two versions stay distinct.
a.merge(b, on="id", suffixes=("_a", "_b"))Keys Must Share a Type
A common surprise: a key stored as a string on one side and an integer on the other will simply never match.
Mind Duplicate Keys
If a key value repeats on both sides, merge produces every matching pair. Rows can multiply unexpectedly.
join Defaults to Left
Unlike merge, the join method uses a left join by default. Pass how to change it when you need to.
left.join(right, how="outer")Pick the Right Tool
Use merge for column keys with full control, and join when both tables are already aligned on their index. 🔧
Quick Check
Your key columns have different names in each table. What do you use?
Recap: Matching the Keys
You can merge on shared columns, mismatched names, or the index, and use suffixes to keep overlapping columns clear. Nicely done!
Domande Frequenti
La lezione «Eseguire il merge su chiavi e indici» è gratuita?
Sì — il testo completo di «Eseguire il merge su chiavi e indici» è 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 «Eseguire il merge su chiavi e indici»?
Unire per colonne o per indice. 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 2 di 4.
Quanto tempo richiede la lezione «Eseguire il merge su chiavi e indici»?
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
- Inner, left, right e outer
- Eseguire il merge su chiavi e indici
- concat per impilare e aggiungere
- Diagnosticare i join errati