Skip to content

Introduction to Data Structures

Pandas provides two primary data structures for working with data:

  • Series (1-dimensional)
  • DataFrame (2-dimensional)

These structures are built upon NumPy arrays, making them efficient for numerical computations and data manipulation.

Think of a DataFrame as a container for multiple Series objects that share the same index. Each column in a DataFrame is a Series.

Data StructureDimensionsDescription
Series1A 1D labeled array capable of holding any data type (integers, strings, floats, Python objects, etc.). Think of it like a labeled list or a single column in a spreadsheet.
DataFrame2A 2D labeled data structure with columns of potentially different types. Similar to a spreadsheet, an SQL table, or a dictionary of Series objects.

Using these structures simplifies many data handling tasks. For instance, when working with tabular data (DataFrames), it’s more intuitive to refer to rows by their index labels and columns by their names, rather than dealing with abstract axis numbers like axis 0 and axis 1.

While Pandas historically had a 3D structure called Panel, it is now less common. For multi-dimensional data beyond 2D, users typically employ MultiIndex DataFrames or libraries like Xarray, which offer more flexible N-dimensional data handling capabilities.

Pandas data structures are generally value-mutable, meaning the data they contain can be modified. DataFrames are also size-mutable, allowing columns to be added or removed. However, Series are size-immutable; their length cannot be changed after creation, although their values can be modified.

Note: The DataFrame is the most widely used and crucial data structure in Pandas for data analysis tasks.

A Series is a one-dimensional labeled array, typically holding data of a single type (though it can hold mixed types, often indicated by an ‘object’ dtype). For example, the following represents a Series of integers:

IndexValue
010
123
256
……
  • Typically homogeneous data (single data type)
  • Size Immutable (length cannot change)
  • Values Mutable (data can be changed)

A DataFrame is a two-dimensional labeled structure, like a table, where columns can have different data types. For example:

NameAgeGenderRating
Steve32Male3.45
Lia28Female4.6
Vin45Male3.9
Katie38Female2.78

This table represents data about a sales team. Each row corresponds to an individual employee (an observation), and each column represents a specific attribute (a feature).

The data types of the columns in the example above might be inferred by Pandas as:

ColumnInferred Type
Nameobject (string)
Ageint64 (integer)
Genderobject (string)
Ratingfloat64 (float)
  • Potentially heterogeneous data (columns can have different types)
  • Size Mutable (columns/rows can be added/removed)
  • Data Mutable (values can be changed)