Python Tutorial: Arithmetic with Series & DataFrames
Skills:
Data Literacy80%
Key Takeaways
This video tutorial covers arithmetic and mathematical operations between Pandas Series and DataFrames, including element-wise multiplication, division, and percentage calculations, as well as handling non-aligned indexes.
Full Transcript
let's explore various arithmetic and mathematical operations between pandas series and data frames we load daily weather measurements for Pittsburgh from 2013 we make date the index and we use par States equals true to get date/time objects with date/time indexes we can use convenient strings to slice say the first week of July from the precipitation in column the precipitation data are in inches let's convert them to centimeters we use the asterisk to multiply a series element-wise by 2.5 for remember we can broadcast standard scalar mathematical operations here broadcasting means the multiplication is applied to all elements in the data frame let's find the percentage variation in temperature in the first week of July that is the daily minimum and the daily maximum temperatures expressed as a percentage of the daily mean friend temperature we can compute this by dividing both the min temperature F and the max temperature F columns by the mean temperature F column and multiplying both by 100 to begin slice the min temperature F and the max temperature F columns as a data frame week one range next slice the mean temperature F column as a series week one mean dividing the data frame week one range by the series week one mean doesn't quite work the column labels don't match so the result has all null values instead we want to use the data frame divide method with the option access equals rows the divide method provides more fine-grained control than the slash operator for division itself this broadcasts the series week 1 mean across each row to produce the desired ratios we can see the temperature range varies by it most about 10% from the mean in that week a related computation is to compute a percentage change along a time series we do this by subtracting the previous day's value from the current day's value and if by the previous day's value the percent change method does precisely this computation for us here we also multiply the resulting series by 100 to yield a percentage value notice the value in the first row is NaN because there is no previous entry finally let's examine how arithmetic operations work between distinct series or data frames with non-aligned indexes which happens often in practice we'll use Olympic medal data from 1896 to 2008 here are the top five bronze medal winning countries the top five silver medal winning countries and the top five gold medal winning countries for that period all three data frames have the same indices for the first three rows United States Soviet Union and United Kingdom by contrast the next two rows are either France Germany or Italy let's compute the total medals awarded to each country we start by adding bronze and silver here we add two series of five rows and get back a series with six rows the index of the sum is the union of the row indices from the original two series arithmetic operation between pandas series are carried out four rows with common index values since Germany does not appear in silver and Italy does not appear in bronze those rows have nan in the sum on examination we see the value 2247 for the United States Row is the sum of 1052 and 1195 from the corresponding rows of the bronze and silver series respectively we can get the same sum bronze plus silver with a method invocation using bronze dot add silver the null values occur in the same places the default fill value is nan when summoned rows fail to align we can modify this behavior using the fill value option of the add method by specifying fill value equals 0 the values of Germany and Italy are no longer null just as the divide method is more flexible than the slash operator for division the add method is more flexible than the plus operator for it adding all three series together yield six rows of output but only three have non null values that is France Germany and Italy are not indexed labels in all three series so each of those rows is NaN in the sum we can also change calls to the dot add method with fill value equals zero to get rid of those null values in the triple sum now you can get some experience with standard arithmetic operations and methods for series and data frames in the exercises
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/merging-dataframes-with-pandas at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Let's explore various arithmetic & mathematical operations between Pandas Series & DataFrames.
We load daily weather measurements for Pittsburgh from 2013.
We make 'Date' the Index & we use parse_dates=True to get datetime objects.
With datetime Indexes, we can use convenient strings to slice, say, the first week of July from the 'PrecipitationIn' column.
The Precipitation data are in inches; let's convert them to centimeters.
We use the asterisk to multiply a Series elementwise by 2-point-54.
Remember, we can broadcast standard scalar mathematical operations.
Here, broadcasting means the multiplication is applied to all entries in the DataFrame.
Let's find the percentage variation in temperature in the first week of July.
That is, the daily minimum & the daily maximum temperatures expressed as a percentage of the daily mean temperature.
We compute this by dividing both the 'Min TemperatureF' and the 'Max TemperatureF' columns by the Mean TemperatureF column and multiplying both by 100.
To begin, slice the 'Min TemperatureF' and the 'Max TemperatureF' columns as a DataFrame week1_range.
Next, slice the Mean TemperatureF column as a Series week1_mean.
Dividing DataFrame week1_range by Series week1_mean doesn't quite work.
The column labels don't match so the result has all null values.
Instead, we want to use the DataFrame dot divide() method with option axis='rows'.
The dot divide() method provides more fine-grained control than the division operator by itself.
This broadcasts the Series week1_mean values across each row to produce the desired ratios.
We can see the temperature range varies by at most about 10% from the mean in that week.
A related computation is to compute a percentage change along a time series.
We do this
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Data Literacy
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
Why I built a simple AI provider wrapper (and you might too)
Dev.to · zhongqiyue
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · AI
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI