Creating DataFrames
Published by Brayan K
Creating a DataFrame means turning raw Python data — dictionaries, lists, or NumPy arrays — into a labelled table using the pd.DataFrame(...) constructor, choosing the input shape that best matches your data.
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Choose whether the outer structure describes columns or rows
A dictionary of lists describes columns. A list of dictionaries describes row records. Both can produce the same visible table, but they have different rules for length, missing keys and labels. Make the input shape explicit before reaching for a constructor.
import pandas as pd
columns = {"item": ["pen", "book"], "quantity": [4, 2]}
records = [{"item": "pen", "quantity": 4}, {"item": "book", "quantity": 2}]
from_columns = pd.DataFrame(columns)
from_records = pd.DataFrame(records, columns=["item", "quantity"])
print(from_columns.to_dict(orient="records"))
print(from_records.to_dict(orient="records"))
print(from_columns.equals(from_records))
# ✅ Expected output:
# [{'item': 'pen', 'quantity': 4}, {'item': 'book', 'quantity': 2}]
# [{'item': 'pen', 'quantity': 4}, {'item': 'book', 'quantity': 2}]
# TrueThe columns argument declares the output column order here. It is not automatically a schema validator: it can select fields and omit fields that were present in input records. The DataFrame: Rows and Columns owns the resulting two-axis table; this lesson owns converting source structures into that table.
A dictionary of value lists must describe equally many rows
import pandas as pd
pd.DataFrame({"item": ["pen", "book"], "quantity": [4]})
# ✅ Expected error:
# ValueError: All arrays must be of the same lengthimport pandas as pd
frame = pd.DataFrame({"branch": "north", "quantity": 0}, index=["pen", "book"])
print(frame.to_dict(orient="records"))
print(frame.index.tolist())
# ✅ Expected output:
# [{'branch': 'north', 'quantity': 0}, {'branch': 'north', 'quantity': 0}]
# ['pen', 'book']import pandas as pd
pd.DataFrame({"branch": "north", "quantity": 0})
# ✅ Expected error:
# ValueError: If using all scalar values, you must pass an indexA scalar repeats for every supplied row label. A length-one list remains a list with one observation; it is not the same contract as a scalar. Decide whether a value is a shared field or a one-row observation instead of changing its container until the constructor stops complaining.
Record keys can create missing cells or silently disappear
import pandas as pd
records = [{"item": "pen", "quantity": 4}, {"item": "book"}]
frame = pd.DataFrame(records, columns=["item", "quantity"])
print(frame["item"].tolist())
print(frame["quantity"].isna().tolist())
print(float(frame.loc[0, "quantity"]))
# ✅ Expected output:
# ['pen', 'book']
# [False, True]
# 4.0import pandas as pd
record = {"item": "pen", "quantity": 4, "quanity": 99}
frame = pd.DataFrame([record], columns=["item", "quantity"])
print(frame.columns.tolist())
print(frame.to_dict(orient="records"))
print("quanity" in record)
# ✅ Expected output:
# ['item', 'quantity']
# [{'item': 'pen', 'quantity': 4}]
# TrueThe misspelled field is still in the source record but does not appear in the selected output. That can be a deliberate projection or a data-loss bug. If extra or missing fields should be rejected, validate the records before constructing the DataFrame. Handling Missing Data (NaN, fillna, dropna) owns policies for missing observations; construction alone cannot choose one for you.
Series inputs align by label instead of zipping by position
import pandas as pd
north = pd.Series([4, 2], index=["pen", "book"], dtype="int64")
south = pd.Series([5, 1], index=["book", "pen"], dtype="int64")
frame = pd.DataFrame({"north": north, "south": south}, index=["pen", "book"])
print(frame.index.tolist())
print(frame.to_dict(orient="records"))
# ✅ Expected output:
# ['pen', 'book']
# [{'north': 4, 'south': 1}, {'north': 2, 'south': 5}]import pandas as pd
values = pd.Series([4, 2], index=["pen", "book"], dtype="int64")
frame = pd.DataFrame({"quantity": values}, index=["first", "second"])
print(frame.index.tolist())
print(frame["quantity"].isna().tolist())
# ✅ Expected output:
# ['first', 'second']
# [True, True]The requested labels first and second are not in the source Series, so their values are missing. Supplying index is not a positional relabelling shortcut for labelled inputs. The Series: Pandas' 1-D Building Block owns arithmetic alignment, and The Index: set_index, reset_index & reindex owns explicit index transformations.
A rectangular array is positional input with its own shape
import numpy as np
import pandas as pd
values = np.array([[4, 1], [2, 5]], dtype="int64")
frame = pd.DataFrame(values, index=["pen", "book"], columns=["north", "south"])
print(values.shape, frame.shape)
print(frame.to_dict(orient="records"))
# ✅ Expected output:
# (2, 2) (2, 2)
# [{'north': 4, 'south': 1}, {'north': 2, 'south': 5}]For this two-dimensional array, the first dimension is rows and the second is columns. Supply exactly one label per corresponding dimension. Unlike a Series input, an ordinary array brings no item labels to align. This lesson makes no promise about shared storage or mutation through the source array; copy and ownership rules depend on the constructor and Pandas version.
import pandas as pd
empty = pd.DataFrame({
"item": pd.Series([], dtype="object"),
"quantity": pd.Series([], dtype="int64"),
})
print(empty.shape)
print(empty.dtypes.astype(str).to_dict())
# ✅ Expected output:
# (0, 2)
# {'item': 'object', 'quantity': 'int64'}An empty batch should not accidentally erase the schema that later code expects. Explicit column types also avoid relying on version-dependent inference defaults. Data Types & astype owns conversion rules and the differences among type families.
Guided exercise: construct columns with an explicit empty schema
Complete the literal hints. The empty table should retain both column names and the declared quantity dtype.
import pandas as pd
frame = pd.___({
# 👉 replace ___ with DataFrame
"item": pd.Series([], dtype="object"),
# 👉 replace ___ with int64
"quantity": pd.Series([], dtype="___"),
})
# 👉 replace ___ with shape
print(frame.___)
print(frame.columns.tolist())
# ✅ Expected output:
# (0, 2)
# ['item', 'quantity']Build brief: validate inventory records before making a table
Implement inventory_frame(records). Require a list of at most 100 dictionaries, each with exactly item and quantity keys. Trim item names, require 1–30 characters and reject duplicates after trimming, case-sensitively. Require a Python int quantity from 0 through 10000; bool must be rejected even though it is an int subclass.
Return a DataFrame in input order with item then quantity columns, an ordinary numbered row index, object item dtype and int64 quantity dtype. The empty list must produce the same two-column schema. Select fresh values without changing the input dictionaries. Reject invalid records before constructing the frame.
import pandas as pd
def inventory_frame(records):
if not isinstance(records, list):
raise TypeError("a list of inventory records is required")
if len(records) > 100:
raise ValueError("at most 100 inventory records")
items, quantities, seen = [], [], set()
for record in records:
if not isinstance(record, dict) or set(record) != {"item", "quantity"}:
raise ValueError("each record needs exactly item and quantity")
item, quantity = record["item"], record["quantity"]
if not isinstance(item, str) or not 1 <= len(item.strip()) <= 30:
raise ValueError("item must contain 1 through 30 characters")
item = item.strip()
if item in seen:
raise ValueError("item names must be unique after trimming")
if type(quantity) is not int or not 0 <= quantity <= 10000:
raise ValueError("quantity must be an integer from 0 through 10000")
seen.add(item)
items.append(item)
quantities.append(quantity)
return pd.DataFrame({
"item": pd.Series(items, dtype="object"),
"quantity": pd.Series(quantities, dtype="int64"),
})
records = [{"item": " pen ", "quantity": 4}, {"item": "book", "quantity": 0}]
frame = inventory_frame(records)
print(frame.to_dict(orient="records"))
print(frame.dtypes.astype(str).to_dict())
print(records[0]["item"] == " pen ")
print(inventory_frame([]).shape)
try:
inventory_frame([{"item": "pen", "quantity": True}])
except ValueError as error:
print(type(error).__name__ + ": " + str(error))
# ✅ Expected output:
# [{'item': 'pen', 'quantity': 4}, {'item': 'book', 'quantity': 0}]
# {'item': 'object', 'quantity': 'int64'}
# True
# (0, 2)
# ValueError: quantity must be an integer from 0 through 10000Test an empty list, the maximum batch size, trimmed duplicates, missing and extra keys, non-dictionary rows, a boolean quantity, a negative quantity and the largest accepted quantity. Construction should follow a domain contract; a rectangular display is not evidence that those conditions were checked.
Practice quiz
What does this program print? import pandas as pd f = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) print(f.to_dict(orient="records"))
- [{'a': 1, 'b': 3}, {'a': 2, 'b': 4}]
- {'a': [1, 2], 'b': [3, 4]}
- [[1, 2], [3, 4]]
- [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
Answer: [{'a': 1, 'b': 3}, {'a': 2, 'b': 4}]. Each resulting record represents one table row.
What does this program print? import pandas as pd f = pd.DataFrame({"x": 7}, index=["a", "b"]) print(f["x"].tolist())
- [7]
- [7, 7]
- [0, 1]
- ['a', 'b']
Answer: [7, 7]. A scalar broadcasts across the explicit index.
What does this program print? import pandas as pd f = pd.DataFrame([{"x": 1}, {}], columns=["x"]) print(f["x"].isna().tolist())
- [False, False]
- [True, True]
- [False, True]
- [True, False]
Answer: [False, True]. The missing record field becomes a missing observation.
What does this program print? import pandas as pd f = pd.DataFrame([{"x": 1, "extra": 2}], columns=["x"]) print(f.columns.tolist())
- ['x', 'extra']
- ['extra']
- []
- ['x']
Answer: ['x']. The columns parameter selects output fields rather than rejecting extras.
What does this program print? import pandas as pd s = pd.Series([4, 2], index=["a", "b"], dtype="int64") f = pd.DataFrame({"x": s}, index=["b", "a"]) print(f["x"].tolist())
- [2, 4]
- [4, 2]
- [None, None]
- ValueError
Answer: [2, 4]. Series construction aligns by the requested labels.
What does this program print? import pandas as pd s = pd.Series([4], index=["a"], dtype="int64") f = pd.DataFrame({"x": s}, index=["z"]) print(f["x"].isna().tolist())
- [False]
- [True]
- [4]
- ['z']
Answer: [True]. An unrelated label has no matching source value.
Why validate exact record keys before a strict import?
- DataFrame forbids dictionaries
- The columns argument validates every source field
- Projection can otherwise hide missing or unexpected fields
- Every extra field is always harmless
Answer: Projection can otherwise hide missing or unexpected fields. The domain decides whether omitted data is acceptable.
Why does inventory_frame explicitly reject bool quantities?
- Pandas cannot represent booleans
- Booleans always become missing
- Every integer is a boolean
- Python bool is an int subclass but is outside the declared quantity contract
Answer: Python bool is an int subclass but is outside the declared quantity contract. A type hierarchy does not establish the domain meaning of a value.
Continue this course
- Previous: The DataFrame: Rows and Columns
- Next: Reading & Writing Data (CSV, Excel, JSON) — Load and save data with read_csv, read_excel, read_json and to_* methods
- Quick reference: Pandas cheat sheet › Creating & Loading