Search⌘ K
AI Features

Similarities in pandas and NumPy

Discover how pandas Series and NumPy arrays share similar behaviors such as index operations and boolean arrays. Learn how to use boolean masks for filtering data efficiently and understand best practices for importing these libraries to improve your Python data workflow.

We'll cover the following...

Similar to NumPy

The Series object behaves similarly to a NumPy array. As shown below, both types respond to index operations:

Python 3.8
import numpy as np
import pandas as pd
songs3 = pd.Series([145, 142, 38, 13], name='counts', index=['Paul', 'John', 'George', 'Ringo'])
numpy_ser = np.array([145, 142, 38, 13])
print(songs3[1])
print(numpy_ser[1])

They both have these methods in common:

Python 3.8
print(songs3.mean())
print(numpy_ser.mean())

They also both have a notion of a boolean array. A boolean array is a Series with the same index as the Series we’re working with and has boolean values. It can be used as a mask to filter out items. Normal Python lists ...