liveBook · Manning

Data frame

As explained in The Quick Python Book, Fourth Edition

A data frame is a two-dimensional tabular data structure with labeled rows and columns that can hold and manipulate heterogeneous data.

A pandas data frame is a two-dimensional grid held in memory, much like a relational database table. It gives data visible row and column labels and supports operations on whole columns or frames, often avoiding the loops needed with lists of lists. 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

A 3 × 3 grid becomes a data frame with numbered rows and named columns. section 24.2.3

Column selection uses the labels:

  • `df["two"]` returns the values in one labeled column; they can be iterated over. section 24.2.3
  • `df[["one", "three"]]` returns another data frame containing both columns. Calling `add(2)` on that result adds 2 to every displayed value. section 24.2.3

Data frames can also be loaded from CSV or JSON. Use `pd.read_csv("temp_data_01.csv", na_values=["Missing"])` to convert that file’s `Missing` entries to `NaN` as it loads. `to_csv(..., index=False)` saves without the row index; `to_json()` produces JSON. Some other input formats depend on libraries outside pandas. section 24.3.1

Clean a percentage column

For a column containing strings such as `'0.00%'`, `temp[17] = pd.to_numeric(temp[17].str.strip("%")).div(100)` removes the percent sign, converts the values to numbers, and expresses them as fractions. The operation applies down the column rather than requiring a row-by-row loop. section 24.3.2

Join and filter sales data

`pd.merge(calls, revenue, on=['Territory', 'Month'])` combines sales-call and revenue frames on their shared columns. From the resulting `calls_revenue`, `calls_revenue[calls_revenue.Territory == 3]` selects one territory; `calls_revenue['Call_Amount'] = calls_revenue.Amount / calls_revenue.Calls` adds a calculated column. section 24.4.1 section 24.4.2

Data frames are useful for loading, cleaning, selecting, and combining tabular data, but pandas is not always the best choice for datasets unsuited to it or for near-real-time processing. section 24.7

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through construction, column selection, and cleaning with small examples rather than storage internals.
Examples
Demonstrates a numeric grid, a weather CSV with percentage strings, and sales calls joined to revenue.
What it assumes
Assumes familiarity with Python lists, indexing, and comprehensions while introducing data frame construction from a simple grid.
What you can do after
Apply it via pandas to clean columns, filter rows, and merge datasets on shared fields.
How it connects
Connects data frames to relational database joins, CSV and JSON file I/O, and missing values.

Also explained in

Compare how 19 books explain data frame →

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.