Skip to content

Series and DataFrames

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.

Create a Series
import pandas as pd
 
s = pd.Series([10, 20, 30], name="scores")
print(s)
Create a Series
import pandas as pd
 
s = pd.Series([10, 20, 30], name="scores")
print(s)

With a custom index

Series with explicit index
import pandas as pd
 
s = pd.Series([10, 20, 30], index=["a", "b", "c"], name="scores")
print(s)
print("Index:", s.index)
Series with explicit 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.indexdf.index is one shared set of row labels for the whole table.

From a dictionary of columns

DataFrame from dict
import pandas as pd
 
df = pd.DataFrame({
    "name": ["Asha", "Ravi", "Meera"],
    "age": [23, 28, 26],
    "city": ["Pune", "Delhi", "Bengaluru"],
})
 
print(df)
DataFrame from dict
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)

DataFrame from records
import pandas as pd
 
rows = [
    {"name": "Asha", "age": 23, "city": "Pune"},
    {"name": "Ravi", "age": 28, "city": "Delhi"},
]
 
df = pd.DataFrame(rows)
print(df)
DataFrame from 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.indexdf.index labels rows
  • df.columnsdf.columns labels columns
Index and columns
import pandas as pd
 
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print("index:", df.index)
print("columns:", df.columns)
Index and 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 (dtypedtype).

Check dtypes
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)
Check dtypes
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 objectobject if it contains mixed types like "N/A""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.

diagram Series vs DataFrame mermaid
A DataFrame is a collection of Series that all share one row index.
sketch Series index → value, DataFrame columns p5.js
A Series pairs each index label with one value; a DataFrame is several Series lined up on a shared index.

🧪 Try It Yourself

Exercise 1 – Series With a Custom Index

Exercise 2 – DataFrame from Records

Exercise 3 – Check Column Dtypes

Next

Continue to Data Inspection (head, tail, info, describe) to quickly get oriented with any new dataset you load.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did