Search⌘ K
AI Features

Math Methods on DataFrames

Explore how to perform basic math operations on pandas DataFrames using methods like add and div, as well as operator symbols. Learn how pandas aligns indexes during these operations and the impact of duplicate index values. This lesson helps you understand how to effectively manipulate data within DataFrames for accurate calculations.

We’ve seen that we can perform math operations on Series objects in pandas. Now, we’ll show how we can do math on DataFrames. We’ll begin by looking at the basic math operations. We’ll use a cleaned-up version of the data:

Python 3.8
url = 'https://github.com/mattharrison/datasets/raw/master/data/siena2018-pres.csv'
df = pd.read_csv(url, index_col=0)
pres = tweak_siena_pres(df)
print(pres)

Index alignment

We can perform math operations on DataFrames. There are the math ...