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 manipulating and analyzing structured data, especially tables. It provides a standard framework for common tasks such as loading, cleaning, filtering, and aggregating data. section 24.2 section 24.2.1

The DataFrame is an in-memory, two-dimensional grid with row and column labels. Select one column by name with df["two"], or select a list of columns with df[["one", "three"]] to get another DataFrame. Operations such as add(2) can apply to every value in a selected frame. section 24.2.3

df = pd.DataFrame(grid, columns=["one", "two", "three"] )
print(df)
 
   one  two  three
0    1    2      3
1    4    5      6
2    7    8      9

Create a DataFrame from a list of lists and give its columns useful names. section 24.2.3

Install pandas with pip if needed, and conventionally import it as pd. In a Jupyter notebook, %matplotlib inline enables inline plots; Matplotlib and NumPy are commonly used alongside pandas. section 24.2.2

A typical data workflow uses these operations:

  • Load with pd.read_csv() or pd.read_json(), and save with methods such as df.to_csv() or df.to_json(). Use na_values in read_csv() to standardize markers such as 'Missing' as NaN. section 24.3.1
  • Clean columns in bulk: strip unwanted characters with string methods, convert values with pd.to_numeric(), and apply arithmetic to the resulting column. section 24.3.2
  • Merge related DataFrames on shared columns with pd.merge(). Filter rows with a condition inside brackets, or assign a calculation to a new column. section 24.4.1 section 24.4.2
  • Summarize columns with methods such as sum(), mean(), and median(), or use groupby() to aggregate by category. Add plot.bar() to a grouped result for a simple Matplotlib plot. section 24.4.3 section 24.5

Clean a percentage column

After loading temperature data into temp, remove the percent sign, convert the column to numbers, and express percentages as fractions: temp[17] = pd.to_numeric(temp[17].str.strip("%")).div(100) section 24.3.2

Merge and summarize sales data

With calls and revenue DataFrames loaded, join on territory and month, then total calls and revenue by month: calls_revenue = pd.merge(calls, revenue, on=['Territory', 'Month']) monthly = calls_revenue[['Month', 'Calls', 'Amount']].groupby(['Month']).sum() print(monthly) section 24.4.1 section 24.4.3

pandas can replace tedious loops and comprehensions, but learning its conventions takes time. Plain Python or another tool may be preferable for data that does not fit pandas structures well, or for large datasets needing little mathematical processing. Some input methods also rely on separately installed libraries, so failures may come from a dependency rather than pandas itself. section 24.6 section 24.3.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through DataFrame creation, cleaning, merging, filtering, aggregation, and simple plotting, while leaving many features to further exploration.
Examples
Uses a numbered grid, Mars weather JSON, temperature CSV, and sales-call and revenue tables.
What it assumes
Builds on Python lists, comprehensions, and data-cleaning experience while introducing DataFrames through a small grid.
What you can do after
Applies pandas to load, clean, merge, filter, summarize, and plot small datasets.
How it connects
Connects DataFrames with relational joins, missing values, aggregation, and Matplotlib plotting.

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.