Introduction to Data Structures
Introduction to Pandas Data Structures
Section titled “Introduction to Pandas 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.
Dimensions & Description
Section titled “Dimensions & Description”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 Structure | Dimensions | Description |
|---|---|---|
| Series | 1 | A 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. |
| DataFrame | 2 | A 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.
Mutability
Section titled “Mutability”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.
Series
Section titled “Series”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:
| Index | Value |
|---|---|
| 0 | 10 |
| 1 | 23 |
| 2 | 56 |
| … | … |
Key Points
Section titled “Key Points”- Typically homogeneous data (single data type)
- Size Immutable (length cannot change)
- Values Mutable (data can be changed)
DataFrame
Section titled “DataFrame”A DataFrame is a two-dimensional labeled structure, like a table, where columns can have different data types. For example:
| Name | Age | Gender | Rating |
|---|---|---|---|
| Steve | 32 | Male | 3.45 |
| Lia | 28 | Female | 4.6 |
| Vin | 45 | Male | 3.9 |
| Katie | 38 | Female | 2.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).
Data Type of Columns
Section titled “Data Type of Columns”The data types of the columns in the example above might be inferred by Pandas as:
| Column | Inferred Type |
|---|---|
| Name | object (string) |
| Age | int64 (integer) |
| Gender | object (string) |
| Rating | float64 (float) |
Key Points
Section titled “Key Points”- Potentially heterogeneous data (columns can have different types)
- Size Mutable (columns/rows can be added/removed)
- Data Mutable (values can be changed)