Building a DataFrame
Overview
A table is a dictionary of columns that share one index.
What You Will Learn in This Lesson
By the end of this lesson, you will be able to:
- Build a DataFrame from a dictionary of columns and from a list of records.
- Read
shape,columns,headandinfoas the first four questions. - Explain why every column shares one index.
- Spot a load that went wrong from its shape alone.
Why This Matters
Every table you meet for the rest of this course is a DataFrame, and almost every confusing result comes from not having looked at it first. The four checks in this lesson take two lines and catch a wrong separator, a missing header and a numeric column read as text.
Step 1: From a dictionary of columns
The keys become column names and the values become the columns. Every column shares the same index, which is what makes a row a row.
Columns first
This is the shape pandas thinks in. A DataFrame is not a list of rows that happens to be rectangular; it is a set of columns that happen to line up.
import pandas as pd
df = pd.DataFrame({
"name": ["Ada", "Grace", "Alan"],
"score": [92, 88, 79],
})
print(df)
print(df.shape)
From records, when that is what you have
A list of dictionaries works too, and it is what an API usually hands you. The union of the keys becomes the columns, and a record missing a key gets NaN.
import pandas as pd
records = [{"name": "Ada", "score": 92}, {"name": "Grace"}]
print(pd.DataFrame(records))
One index, shared
Both columns are Series over the same index. Pull one out and the index comes with it — which is why a filtered column still knows which rows it came from.
import pandas as pd
df = pd.DataFrame({"name": ["Ada", "Grace"], "score": [92, 88]})
print(df["score"].index)
print(df.index)
Mini Practice #1: Two ways in
Try It YourselfBuild the same table twice — once from a dictionary of columns and once from a list of records — and check the shapes match.
Step 2: The first four things to ask it
shape, columns, head and info answer "what am I holding" before you do anything else. Skipping them is where most confusing results begin.
How much, and what of
Shape first, because a number you did not expect here means the rest is not worth reading yet.
import pandas as pd
df = pd.DataFrame({"name": ["Ada"], "score": [92]})
print(df.shape)
print(list(df.columns))
print(df.head())
info answers two questions at once
It gives the dtype of every column and how many non-null values each holds, which is the fastest way to see both a text column that should be numeric and a column full of holes.
import pandas as pd
df = pd.DataFrame({"score": ["92", "88"], "n": [1, None]})
df.info()
Key idea: A one-column DataFrame where you expected six is the signature of the wrong separator. The shape told you before any of the numbers did.
Mini Practice #2: Four questions
Try It YourselfAsk this table all four questions in turn. One of the columns is not the type you would want.
Step 3: When to Use It
Several columns about the same rows
A dictionary of equal-length lists, or a list of records, becomes a table with one index.
df = pd.DataFrame({"name": names, "score": scores})
Data from code rather than a file
Test cases, small reference tables, results you computed and want to summarise.
When Not to Use It
Do not build a DataFrame by appending one row at a time in a loop — it copies the table every time. Collect rows in a list first and build once.
End-of-Lesson Exercises
Exercise 1: Describe a table
Write describe_table(df) that returns a tuple of the number of rows, the number of columns, and the column names as a list.
df.shape is a tuple of rows and columns. list(df.columns) turns the column index into plain names.
Exercise 2: One index, shared by every column
Write labelled(columns, labels) that builds a DataFrame from a dictionary of columns, using labels as the index, and returns the index labels as a list. Every column shares that one index.
pd.DataFrame takes an index= argument. Build the table with it, then return list(df.index) rather than handing back the labels you were given.