Python libraries for data

Pandas, NumPy and Matplotlib.

Computer Science/Informatics Practices5 questions

The essentials

Python has free libraries that make data work easy. NumPy (Numerical Python) provides the ndarray, a fast multi-dimensional array. Pandas (from "panel data") provides the Series and DataFrame structures for labelled data, and functions to read, clean, analyse and summarise it. Matplotlib is used to draw graphs and charts; its pyplot module is used most. These libraries are usually imported as: import numpy as np, import pandas as pd and import matplotlib.pyplot as plt.

Key points

  • A NumPy array stores elements of the same data type (homogeneous). np.array([1, 2.5]) stores both values as floats.
  • Arithmetic on arrays is element-wise: np.array([1, 2, 3]) * 2 gives [2 4 6], whereas the list [1, 2, 3] * 2 gives [1, 2, 3, 1, 2, 3].
  • np.arange(1, 10, 3) gives [1 4 7]; np.zeros(), np.ones() and np.linspace() create arrays.
  • Array attributes: ndim (number of dimensions), shape (size along each dimension), size (total elements) and dtype (data type). np.array([[1, 2, 3], [4, 5, 6]]).shape is (2, 3).
  • Pandas Series: one-dimensional, labelled, can hold any data type.
  • Pandas DataFrame: two-dimensional, labelled rows and columns; different columns can have different data types.
  • Pandas is built on NumPy, and data in a DataFrame can be plotted directly with Matplotlib.
  • Libraries are installed with pip, for example pip install pandas.

Exam tips

  • Know which structure fits: a single column of values → Series; a table → DataFrame; fast numeric arrays → NumPy.
  • Contrast list and array behaviour for + and *.

Common mistakes

  • Thinking all columns of a DataFrame must have the same type; only a NumPy array needs that.
  • Writing import pandas as np; the conventional aliases are pd for Pandas and np for NumPy.

Practice questions

Practice this topic