Creating a Series
A Series is a labeled 1D array. Think of it as a single spreadsheet column: every value has a matching row label sitting to its left, called the index.
import pandas as pd
s = pd.Series([10, 20, 30], name="scores")
print(s)With a custom index
import pandas as pd
s = pd.Series([10, 20, 30], index=["a", "b", "c"], name="scores")
print(s)
print("Index:", s.index)Creating a DataFrame
A DataFrame is a labeled 2D table — literally a dictionary of Series that all share the
same row index. That's why every column can have a different dtype, but df.index is one
shared set of row labels for the whole table.
From a dictionary of columns
import pandas as pd
df = pd.DataFrame({
"name": ["Asha", "Ravi", "Meera"],
"age": [23, 28, 26],
"city": ["Pune", "Delhi", "Bengaluru"],
})
print(df)From a list of dictionaries (records)
import pandas as pd
rows = [
{"name": "Asha", "age": 23, "city": "Pune"},
{"name": "Ravi", "age": 28, "city": "Delhi"},
]
df = pd.DataFrame(rows)
print(df)Understanding index and columns
df.indexlabels rowsdf.columnslabels columns
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print("index:", df.index)
print("columns:", df.columns)Dtypes: why they matter
Pandas stores each column using a data type (dtype).
import pandas as pd
df = pd.DataFrame({
"age": [20, 21, 22],
"score": [90.5, 88.0, 91.0],
"passed": [True, True, False],
})
print(df.dtypes)Common dtype pitfalls
- A numeric column can become
objectif it contains mixed types like"N/A". - Dates start as strings unless you parse them.
We’ll fix these issues later in the phase.
Visualize it
A Series is a single labeled column; a DataFrame stacks several Series side by side that all share the same row index.
flowchart LR S1["Series: name"] --> DF["DataFrame"] S2["Series: age"] --> DF S3["Series: city"] --> DF DF --> IDX["Shared index (row labels)"]
🧪 Try It Yourself
Exercise 1 – Series With a Custom Index
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – DataFrame from Records
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Check Column Dtypes
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
How does the lesson describe a DataFrame?
Each column can have its own dtype, but df.index is one shared set of row labels.
Show answer
A — A dictionary of Series that share the same row index — Each column can have its own dtype, but df.index is one shared set of row labels.
What does pd.Series([10, 20, 30], index=["a", "b", "c"]) give you?
The index argument sets the row labels, which you can read with s.index.
Show answer
A — A Series whose labels are a, b and c — The index argument sets the row labels, which you can read with s.index.
Which input builds a DataFrame from a list of records?
pd.DataFrame(rows) turns each dict into a row, using the keys as column names.
Show answer
A — A list of dicts, one dict per row — pd.DataFrame(rows) turns each dict into a row, using the keys as column names.
A numeric column contains the string N/A in one cell. What dtype will it likely have?
Mixed types push a column to object. The lesson lists this as a common dtype pitfall.
Show answer
A — object — Mixed types push a column to object. The lesson lists this as a common dtype pitfall.
Next
Continue to Data Inspection (head, tail, info, describe) to quickly get oriented with any new dataset you load.