Lesson 6 of 6 · about 20 minutes
Renaming and combining
Renaming columns
Column names from files are often long, inconsistent or unclear. rename() changes them. Give it a dictionary of old name to new name:
import pandas as pd
df = pd.DataFrame({"Student Name": ["Ava", "Ben"], "pts": [88, 92]})
df = df.rename(columns={"Student Name": "name", "pts": "points"})
print(df)The output:
name points
0 Ava 88
1 Ben 92Like most pandas methods, rename() returns a new table and leaves the old one alone, which is why the result is assigned back to df.
Renaming rows
The same method renames row labels with index=:
import pandas as pd
df = pd.DataFrame({"points": [88, 92]}, index=["a", "b"])
print(df.rename(index={"a": "Ava", "b": "Ben"}))The output:
points
Ava 88
Ben 92To give the index itself a name, use rename_axis("student"). And df.columns = [...] replaces all column names at once, as long as you give the right number of names.
Stacking tables: concat()
When two tables have the same columns, for example January's sales and February's, pd.concat() puts one under the other:
import pandas as pd
jan = pd.DataFrame({"shop": ["North", "South"], "boxes": [12, 7]})
feb = pd.DataFrame({"shop": ["North", "South"], "boxes": [15, 10]})
both = pd.concat([jan, feb], ignore_index=True)
print(both)The output:
shop boxes
0 North 12
1 South 7
2 North 15
3 South 10ignore_index=True numbers the rows again from 0. Without it, both tables keep their own row numbers, so you would get 0, 1, 0, 1.
Quick check
What does ignore_index=True do in pd.concat()?
Joining tables: merge()
When two tables share a column, merge() lines up the rows that match, like a lookup in a spreadsheet:
import pandas as pd
students = pd.DataFrame({"id": [1, 2, 3], "name": ["Ava", "Ben", "Cleo"]})
scores = pd.DataFrame({"id": [1, 2, 4], "score": [88, 92, 70]})
print(students.merge(scores, on="id"))
print()
print(students.merge(scores, on="id", how="left"))The output:
id name score
0 1 Ava 88
1 2 Ben 92
id name score
0 1 Ava 88.0
1 2 Ben 92.0
2 3 Cleo NaNBy default merge() keeps only the ids found in both tables (an "inner" join), so Cleo (no score) and id 4 (no name) are left out. how="left" keeps every row of the left table and fills the gaps with NaN. how="outer" keeps everything from both.
df.join(other) does the same kind of matching using the row labels (the index) instead of a column.
Quick check
Two tables share an id column. The left has ids 1, 2, 3 and the right has 2, 3, 4. How many rows does a default merge on id give?
Exercise 1: Better names
Rename the columns of df: Q1 becomes name and Q2 becomes age. Keep the result in df and print it.
Exercise 2: Stack the months
Combine march and april into one DataFrame called spring, with the rows numbered from 0 to 3. Print it.
Exercise 3: Who ordered what
Merge orders with customers on customer_id into a DataFrame called full, so every order also has the customer's name. Print it.
Lesson 6 complete
You finished renaming and combining and passed every exercise.
What you learned
df.rename(columns={...})anddf.rename(index={...})rename columns and rows.pd.concat([a, b], ignore_index=True)stacks tables with the same columns.a.merge(b, on="key")joins tables on a shared column;how="left"orhow="outer"keeps unmatched rows.join()matches on the index.
You now know the core of pandas: making and loading tables, selecting, summarising, grouping, cleaning and combining them. Try it on a real dataset: a CSV of your own, or one from a site like data.gov.uk.