Python Central HubSign in

Data Analytics with Python / Phase 3: Data Manipulation with Pandas

Series and DataFrames

Learn how to create Series and DataFrames, inspect their structure, and understand labels, dtypes, and indexes.

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.

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

With a custom index

Python
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

Python
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)

Python
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.index labels rows
  • df.columns labels columns
Python
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).

Python
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 object if 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.

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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – DataFrame from Records

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Check Column Dtypes

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. How does the lesson describe a DataFrame?

    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.

  2. What does pd.Series([10, 20, 30], index=["a", "b", "c"]) give you?

    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.

  3. Which input builds a DataFrame from a list of records?

    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.

  4. A numeric column contains the string N/A in one cell. What dtype will it likely have?

    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.

Was this page helpful?

Let us know how we did