CodeMIRAI
0 XP

Introduction to pandas

2 min read 3 runnable examples Quiz included

pandas is the standard Python library for working with tables of data: spreadsheets, CSV files, database results. If you've used Excel, think of pandas as Excel you can program, with no row limit.

Python
import pandas as pd

df = pd.DataFrame({
    "city": ["Mumbai", "London", "Osaka", "Madrid"],
    "population_m": [20.7, 9.6, 19.0, 6.8],
    "country": ["India", "UK", "Japan", "Spain"],
})
print(df)

import pandas as pd is the universal convention.

The two building blocks

  • A DataFrame is a whole table: rows and named columns. Each column can have its own type.
  • A Series is a single column: values with an index (row labels).
Python
import pandas as pd

df = pd.DataFrame({
    "city": ["Mumbai", "London", "Osaka", "Madrid"],
    "population_m": [20.7, 9.6, 19.0, 6.8],
})
pop = df["population_m"]      # one column is a Series
print(type(df).__name__, type(pop).__name__)
print(pop)
print("Average:", pop.mean())

A first analysis

A few lines answer real questions:

Python
import pandas as pd

sales = pd.DataFrame({
    "region": ["North", "South", "North", "East", "South", "East", "North"],
    "product": ["Laptop", "Phone", "Phone", "Laptop", "Laptop", "Phone", "Laptop"],
    "amount": [1200, 650, 700, 1150, 1300, 600, 1250],
})

print(sales.head(3))                              # first rows
print("\nTotal sales:", sales["amount"].sum())
print("\nBy region:")
print(sales.groupby("region")["amount"].sum().sort_values(ascending=False))

Each of those steps (looking at rows, summing, grouping) gets its own lesson in this course.

What pandas is used for

  • Cleaning messy data: missing values, wrong types, duplicates
  • Exploring: summaries, filtering, sorting
  • Combining tables, like a SQL join
  • Reshaping and aggregating for reports
  • Plotting with one line, via matplotlib

pandas sits in the middle of a set of tools:

ToolHow it works with pandas
Jupyternotebooks, where most pandas work happens
NumPythe arrays under every column
matplotlibdraws the charts behind df.plot()
Excelread_excel() and to_excel() move tables in and out
SQL databasesread_sql() turns a query's result into a DataFrame
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.