liveBook · Manning

Pandas

As explained in The Quick Python Book, Fourth Edition

pandas is an open-source Python library for data analysis that provides labeled data structures and operations for manipulating tabular and other structured data.

pandas is a Python library for working with tabular data. Its central structure, the data frame, holds a two-dimensional grid with row and column labels. It makes repeated tasks—loading, cleaning, selecting, combining, and summarizing data—less cumbersome than writing loops and list comprehensions for each task. section 24.2.1 section 24.2.3

Start with a data frame

Given `grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]`, create `df = pd.DataFrame(grid, columns=["one", "two", "three"])` after `import pandas as pd`. Then `df["two"]` selects the values 2, 5, and 8 by column name. `df[["one", "three"]]` returns another data frame containing those two columns; adding `.add(2)` adds 2 to every selected value. The double brackets matter: they pass a list of column names. section 24.2.3

Common operations fit into a short workflow:

  • Load a file with `pd.read_csv(...)` or `pd.read_json(...)`. For CSV data, `na_values=['Missing']` converts that marker to `NaN` during loading; `usecols` can omit unwanted columns. Save a frame with `to_csv(...)` or `to_json(...)`. section 24.3.1 section 24.3.2
  • Clean a column with column-wide operations. For percentage strings, `temp[17] = pd.to_numeric(temp[17].str.strip('%')).div(100)` removes the sign, converts the values to numbers, and expresses them as fractions. section 24.3.2
  • Combine related frames with `pd.merge(calls, revenue, on=['Territory', 'Month'])`. A shared pair of columns links sales calls to revenue, much like a relational database join. section 24.4.1
  • Filter rows with a condition, such as `calls_revenue[calls_revenue.Territory == 3]`. Calculate a new column with `calls_revenue['Call_Amount'] = calls_revenue.Amount / calls_revenue.Calls`. section 24.4.2
  • Summarize a column with methods such as `sum()`, `mean()`, or `median()`, and use `groupby(...)` to calculate summaries per month or territory. section 24.4.3
  • Plot a grouped frame in a Jupyter notebook with `.plot.bar()`; pandas uses Matplotlib for the plot. `plot.line()` and `plot.pie()` offer other simple views. section 24.5
print(calls_revenue[['Month', 'Calls', 'Amount']].groupby(['Month']).sum())
 
       Calls  Amount
Month               
1        318  141165
2        302  164279
3        267  132238
4        328  135201
 
print(calls_revenue[['Territory', 'Calls', 'Amount']].groupby(['Territory']).sum())
 
           Calls  Amount
Territory               
1            444  211532
2            379  149931
3            392  211420

Compare the two grouped outputs: the same calls and revenue columns yield totals by month or by territory simply by changing the grouping column. section 24.4.3

Install pandas with `pip install pandas`; a common import is `import pandas as pd`. In a Jupyter notebook, `%matplotlib inline` enables inline Matplotlib plots. Some file and database operations rely on additional libraries, so an import problem may lie outside pandas itself. section 24.2.2 section 24.3.1

Choose pandas for data that fits its tabular approach and benefits from these operations—not automatically for every dataset. Learning its conventions takes time, and plain Python may suit large datasets needing little mathematical manipulation, awkwardly structured data, or basic transaction-stream processing better. section 24.6

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through data frames and practical operations with small examples, while leaving the wider library and deeper visualization options for further study.
Examples
Uses a numeric grid, weather CSV and JSON data, and linked sales-call and revenue tables.
What it assumes
Assumes general Python familiarity, including lists and comprehensions, while introducing data frames through a small grid.
What you can do after
Apply it via pandas to load and clean files, select and join rows, calculate grouped totals, and make simple plots.
How it connects
Connects data frames with CSV, JSON, missing values, relational joins, grouped aggregation, Matplotlib, and Jupyter notebooks.

Also explained in

Compare how 11 books explain pandas →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.