liveBook · Manning

Data frame

As explained in Grokking Machine Learning

A DataFrame is a two-dimensional labeled tabular data structure provided by Pandas for storing and manipulating heterogeneous columns.

A DataFrame is Pandas’ object for storing a table with multiple columns. A Series holds one column, while a DataFrame holds several columns and rows. section 13.1

Load a CSV into a DataFrame with pandas.read_csv(). For the Titanic data, raw_data contains passenger details such as class, age, fare, cabin, embarkation, and survival. Pandas adds row numbering by default; index_col="PassengerId" instead uses the dataset’s passenger identifiers as the index. section 13.1

Once loaded, inspect and select the table with simple operations:

  • Use len(raw_data) to count rows and raw_data.columns to inspect column labels. section 13.1
  • Select one column as a Series with raw_data['Survived'], or select several columns with raw_data[['Name', 'Age']]. section 13.1
  • Save a DataFrame to CSV and load it again for later cleaning or preprocessing. section 13.1
Table 13.1 The Titanic dataset contains information on the passengers on the Titanic, including whether they survived. Here we use Pandas to open the dataset and print out its rows and columns. Notice that it has 891 rows and 12 columns. (view table figure)

PassengerId

Survived

Pclass

Name

Sex

Age

SibSp

Parch

Ticket

Fare

Cabin

Embarked

1

0

3

Braund, Mr. Owen Harris

male

22.0

1

0

A/5 21171

7.2500

NaN

S

2

1

1

Cumings, Mrs. John Bradley (Florence Briggs Th...

female

38.0

1

0

PC 17599

71.2833

C85

C

3

1

3

Heikkinen, Miss Laina

female

26.0

0

0

STON/O2. 3101282

7.9250

NaN

S

...

...

...

...

...

...

...

...

...

...

...

...

890

1

1

Behr, Mr. Karl Howell

male

26.0

0

0

111369

30.0000

C148

C

891

0

3

Dooley, Mr. Patrick

male

32.0

0

0

370376

7.7500

NaN

Q

Browse the Titanic table to see its labeled rows, twelve columns, missing cabin values, and passenger records. section 13.1

How Grokking Machine Learning covers it

How deep it goes
Defines DataFrames, loads one from CSV, explains indexing, and demonstrates inspection and column selection.
Examples
Uses the 891-row, 12-column Titanic passenger dataset, including survival, age, fare, cabin, and embarkation columns.
What it assumes
Assumes basic Python and CSV familiarity while introducing Pandas imports, DataFrames, Series, and indexed tables.
What you can do after
Operate Pandas to load, inspect, select, save, and reload a tabular dataset.
How it connects
Connects DataFrames with Pandas, Series, CSV files, indexing, data cleaning, preprocessing, and machine learning.

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 Grokking Machine Learning. The section links point to the parts of the book it draws on.