Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · PANDAS · CHAPTER 6

DataFrames

On this page

While a Series represents a single labeled column, a DataFrame represents a full table — with both rows and columns, much like an Excel sheet or SQL table.

A DataFrame can be understood as a collection of Series sharing the same index. Each column is a Series, and together they form a structured dataset.

Let’s create one using our earlier example of cultural funding across decades.

import pandas as pd
data = {
    '1990': [120, 80, 90],
    '2000': [150, 100, 130],
    '2010': [180, 120, 170]
}

countries = ['Aland', 'Bretoria', 'Caldara']

funding_df = pd.DataFrame(data, index=countries)
print(funding_df)
          1990  2000  2010
Aland      120   150   180
Bretoria    80   100   120
Caldara     90   130   170

This DataFrame holds three columns (for decades) and three rows (for countries). Each cell contains one measurement — for instance, Aland’s funding in 2010 is 180.

Accessing Data in a DataFrame

Data can be accessed using either column labels or row indices.

Accessing columns

print(funding_df['2000'])
Aland       150
Bretoria    100
Caldara     130
Name: 2000, dtype: int64

Accessing rows

Rows can be accessed with .loc (by label) or .iloc (by index position):

print("Access by label:\n", funding_df.loc['Aland'])
print("Access by position:\n", funding_df.iloc[0])
Access by label:
 1990    120
2000    150
2010    180
Name: Aland, dtype: int64
Access by position:
 1990    120
2000    150
2010    180
Name: Aland, dtype: int64

Both return Aland’s row of data.

Simple Analysis

Once data are stored in a DataFrame, statistical operations become very straightforward.

print("Average funding per year:", funding_df.mean())

print("\nMaximum funding by country:", funding_df.max(axis=1))
Average funding per year: 1990     96.666667
2000    126.666667
2010    156.666667
dtype: float64

Maximum funding by country: Aland       180
Bretoria    120
Caldara     170
dtype: int64

Here, funding_df.mean() computes the mean along columns (per decade), while funding_df.max(axis=1) finds the maximum across rows (per country).

Interpreting DataFrames in Context

In Digital Humanities, DataFrames are an essential structure for representing complex information. Each row could represent a text, author, or event, while columns might represent word counts, themes, or sentiment values.

For instance:

  • Rows = literary works
  • Columns = linguistic or stylistic features
  • Values = frequency counts or computed scores

This structure allows researchers to apply both computational and statistical techniques while maintaining a human-readable form.

Summary

The Pandas library provides two fundamental structures for data handling:

  • The Series, for labeled one-dimensional data.
  • The DataFrame, for multi-dimensional, table-like data.

These two objects form the foundation of modern data analysis in Python. Together, they make it possible to load, clean, explore, and transform data efficiently — preparing it for visualization and deeper analysis, which we will explore in the next chapters.

Teaching edition · verified experiments · PDF and EPUB editions not generated.