liveBook · Manning

Data frame

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A pandas DataFrame is a two-dimensional labeled tabular data structure organized into rows and columns.

A pandas DataFrame is a two-dimensional table of labeled rows and columns, much like a spreadsheet. Each column is a Series, all columns share one index, and columns can hold different dtypes. Think of it as a collection of aligned Series describing the same records. section 2.0 section 1.0

Figure 1.1 Each of a data frame’s columns is a series.

View the column-oriented structure: every DataFrame column is a Series. section 1.0

Rows represent records; columns represent attributes. Add a column to give every record a new aspect, or add a row to introduce a new record. Before answering a question, narrow the table to the relevant rows and columns. section 2.0

Create a DataFrame in forms that fit the data at hand:

  • Use a list of lists when each inner list is a complete row and values fill columns positionally. section 2.2
  • Use a list of dictionaries when describing row-like records; dictionary keys supply column names. section 2.2
  • Use a dictionary of lists or Series when the data already arrives by column; Series must have matching lengths. section 2.2 section 2.18
  • Use a two-dimensional NumPy array when the source is already a rectangular array. section 2.2

Calculate product net revenue

Select price and sales columns, subtract wholesale_price from retail_price row by row, multiply by sales, then sum the resulting Series. Matching indexes keep the column arithmetic aligned with the original rows. section 2.2

Use df['column'] to retrieve a column as a Series, and use .loc with labels or boolean conditions to retrieve or update selected rows and columns. A boolean condition can filter a table, while .loc can assign many matching cells at once. section 2.0 section 2.10

DataFrames are meant to change during analysis. Assigning to a new column name adds a derived column; assigning to an existing name replaces it. df.assign creates a new DataFrame, which supports readable method chains. section 2.6

Combine and repair product records

Create rows for new products, combine them with pd.concat, inspect missing sales values through .loc, and assign replacement values to the selected rows. Adding missing values can change an integer column to float because NaN is a float value. section 2.10

Importing with pd.read_csv creates a new DataFrame from a file or URL. Limit imported columns with usecols, set names when headers are absent, and choose dtypes when compact storage matters. Concatenate imported tables after adding a year column to preserve their origin. section 3.1 section 3.6

Missing data deserves a deliberate policy. df.dropna returns a new table without rows containing missing values, and subset limits which columns determine removal. Count total rows with len(df.index), because count ignores NaN values. section 5.2

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Works through construction, indexed selection, column arithmetic, concatenation, missing values, and CSV import without yet using grouping or date-time parsing.
Examples
Uses product inventory revenue, taxi trips, temperatures, cryptocurrency prices, Unix passwd records, and parking violations.
What it assumes
Builds on Series and indexes while introducing two-dimensional tables and common DataFrame tasks.
What you can do after
Apply it via pandas to create, filter, update, combine, import, and clean labeled tables.
How it connects
Connects DataFrames to Series, indexes, boolean indexing, .loc, pd.concat, pd.read_csv, dtypes, 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 Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.