Series and DataFrames
Creating a Series
Section titled “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
Section titled “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
Section titled “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
Section titled “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)
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “🧪 Try It Yourself”Exercise 1 – Series With a Custom Index
Section titled “Exercise 1 – Series With a Custom Index”Exercise 2 – DataFrame from Records
Section titled “Exercise 2 – DataFrame from Records”Exercise 3 – Check Column Dtypes
Section titled “Exercise 3 – Check Column Dtypes”Continue to Data Inspection (head, tail, info, describe) to quickly get oriented with any new dataset you load.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading