Table of Contents
ToggleData structures in Pandas are designed to handle data efficiently. They allow for the organization, storage, and modification of data in a way that optimizes memory usage and computational performance. The Python Pandas library provides two primary data structures for handling and analyzing data:
In general programming, the term “data structure” refers to the method of collecting, organizing, and storing data to enable efficient access and modification. Data structures are collections of data types that provide the best way of organizing items (values) in terms of memory usage.
Pandas is built on top of NumPy and integrates well within a scientific computing environment with many other third-party libraries. This tutorial by Vista Academy will provide a detailed introduction to these data structures.
| Data Structure | Dimensions | Description |
|---|---|---|
| Series | 1 | A one-dimensional labeled homogeneous array, size immutable. |
| DataFrame | 2 | A two-dimensional labeled, size-mutable tabular structure with potentially heterogeneously typed columns. |
Working with two or more dimensional arrays can be complex and time-consuming, as users need to carefully consider the data’s orientation when writing functions. However, Pandas simplifies this process by reducing the mental effort required. For example, when dealing with tabular data (DataFrame), it’s easier to think in terms of rows and columns instead of axis 0 and axis 1.
All Pandas data structures are value mutable, meaning their contents can be changed. However, their size mutability varies:
A Series is a one-dimensional labeled array that can hold any data type. It can store integers, strings, floating-point numbers, etc. Each value in a Series is associated with a label (index), which can be an integer or a string.
| Name | Steve |
|---|---|
| Age | 35 |
| Gender | Male |
| Rating | 3.5 |
import pandas as pd data = ['Steve', '35', 'Male', '3.5'] series = pd.Series(data, index=['Name', 'Age', 'Gender', 'Rating']) print(series)
Output:
Name Steve Age 35 Gender Male Rating 3.5 dtype: object
A DataFrame is a two-dimensional labeled data structure with columns that can hold different data types. It is similar to a table in a database or a spreadsheet.
Consider the following data representing the performance rating of a sales team:
| Name | Age | Gender | Rating |
|---|---|---|---|
| Steve | 32 | Male | 3.45 |
| Lia | 28 | Female | 4.6 |
| Vin | 45 | Male | 3.9 |
| Katie | 38 | Female | 2.78 |
import pandas as pd
# Data represented as a dictionary
data = {
'Name': ['Steve', 'Lia', 'Vin', 'Katie'],
'Age': [32, 28, 45, 38],
'Gender': ['Male', 'Female', 'Male', 'Female'],
'Rating': [3.45, 4.6, 3.9, 2.78]
}
# Creating the DataFrame
df = pd.DataFrame(data)
# Display the DataFrame
print(df)
Output:
Name Age Gender Rating
0 Steve 32 Male 3.45
1 Lia 28 Female 4.60
2 Vin 45 Male 3.90
3 Katie 38 Female 2.78
Pandas data structures are flexible containers for lower-dimensional data. For instance, a DataFrame is a container for Series, and a Series is a container for scalars. This flexibility allows for efficient data manipulation and storage.
Building and handling multi-dimensional arrays can be boring and require careful consideration of the data’s orientation when writing functions. Pandas reduces this mental effort by providing intuitive data structures.
import pandas as pd
# Data represented as a dictionary
data = {
'Name': ['Steve', 'Lia', 'Vin', 'Katie'],
'Age': [32, 28, 45, 38],
'Gender': ['Male', 'Female', 'Male', 'Female'],
'Rating': [3.45, 4.6, 3.9, 2.78]
}
# Creating the DataFrame
df = pd.DataFrame(data)
# Display a Series within a DataFrame
print(df['Name'])
Output:
0 Steve 1 Lia 2 Vin 3 Katie Name: Name, dtype: object
