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 data frame is a two-dimensional, labeled grid for manipulating tabular data in memory. In pandas, it can hold different kinds of values and offers column operations that make selection and cleaning more concise than working with nested lists. section 24.2.3 section 24.3.1

Create a pandas data frame from a list of lists. Rows and columns initially have numeric labels; the columns argument supplies more useful names. 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

The named columns make the grid easier to read and select from. section 24.2.3

Select and operate on columns:

  • df["two"] selects one column, whose values can be iterated over. section 24.2.3
  • df[["one", "three"]] selects multiple columns and returns another data frame. section 24.2.3
  • edges.add(2) adds 2 to every value in the selected numeric frame. section 24.2.3

Load data with pd.read_csv() or pd.read_json(). For CSV input, na_values can turn markers such as "Missing" into NaN. Save a frame with methods such as to_csv() and to_json(); to_csv(..., index=False) omits the row index. section 24.3.1 section 24.3.1

Clean a percentage column

For a temperature frame temp, temp[17] = pd.to_numeric(temp[17].str.strip("%")).div(100) removes percent signs, converts the strings to numbers, and expresses percentages as fractions. For example, "0.00%" becomes 0.0. section 24.3.2

Combine related frames with pd.merge(). Given calls and revenue frames, pd.merge(calls, revenue, on=['Territory', 'Month']) creates a frame joining their columns on the two shared fields. Merges can also handle one-to-many and many-to-many relationships, and left and right joins. section 24.4.1

Filter rows by placing a condition inside brackets. For example, calls_revenue[calls_revenue.Territory == 3] keeps rows for territory 3. A calculation can also become a new column: calls_revenue['Call_Amount'] = calls_revenue.Amount / calls_revenue.Calls. section 24.4.2

Pandas may not be the best choice for datasets that are poorly suited to it or require near-real-time processing. Some input operations also depend on other libraries, which may be the source of errors. section 24.7 section 24.3.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Demonstrates construction, column operations, file input and output, cleaning, merging, and filtering without explaining data-frame internals.
Examples
Uses a numeric grid, Mars weather JSON, temperature CSV data, and sales-call and revenue tables.
What you can do after
Apply data frames via pandas to select and clean data, merge tables, and filter rows.
How it connects
Connects data frames to relational database joins, missing data, aggregation, and grouping.

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.