liveBook · Manning

Data frame

As explained in Natural Language Processing in Action, Second Edition

A DataFrame is a tabular data structure organized into labeled rows and columns that may contain heterogeneous values.

A DataFrame organizes tabular data into labeled rows and columns. Each column represents a dimension, such as a vocabulary term, while cells hold values such as token counts. Columns may contain different kinds of values, and pandas aligns labels while handling missing entries. section 3.2

A DataFrame is useful when you need to:

  • Convert a list of dictionaries into columns automatically. section 3.2
  • Represent documents as rows and vocabulary terms as dimensions. section 3.2
  • Replace missing dictionary keys with NaN, then clean values with fillna(0).astype(int). section 3.2

Document-term counts

Counting tokens for 16 short documents produces a DataFrame with 16 rows and 246 vocabulary columns. A row such as df.iloc[10] exposes the counts for one document, while df.columns contains the vocabulary. section 3.2

Listing 3.1 Short documents about bias
>>> docs = [nlp(s) for s in bias_intro.split('\n')
...         if s.strip()]                    #1
>>> counts = []
>>> for doc in docs:
...     counts.append(Counter([
...         t.text.lower() for t in doc]))    #2
>>> df = pd.DataFrame(counts)
>>> df = df.fillna(0).astype(int)    #3
>>> len(df)
16
>>> df.head()
  algorithmic bias describes  systematic  ... between  same service
0           1    1         1           1  ...       0     0       0
1           0    1         0           0  ...       0     0       0
2           1    1         0           0  ...       0     0       0
3           1    1         0           1  ...       0     0       0
4           0    1         0           0  ...       0     0       0

Inspect a runnable construction that counts tokens, creates a DataFrame, fills missing values, and displays its shape and first rows. section 3.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains construction, labels, missing values, row inspection, and high-dimensional vocabulary columns without descending into implementation internals.
Examples
Uses short bias-related documents, token counters, vocabulary columns, df.head(), and df.iloc[10] to inspect results.
What it assumes
Assumes familiarity with Python dictionaries and token counts while demonstrating missing-value cleanup and labeled vector dimensions.
What you can do after
Build it from scratch by converting token-count dictionaries into a cleaned, inspectable pandas DataFrame.
How it connects
Connects DataFrames with pandas Series, Python dictionaries, Counter, tokenization, vector representations, and term frequency.

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 Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.