w3resource

Pandas: Add, subtract, multiple and divide two Pandas Series

Pandas: Data Series Exercise-3 with Solution

Write a Pandas program to add, subtract, multiple and divide two Pandas Series.

Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 9]

Sample Solution :

Python Code :

import pandas as pd
ds1 = pd.Series([2, 4, 6, 8, 10])
ds2 = pd.Series([1, 3, 5, 7, 9])
ds = ds1 + ds2
print("Add two Series:")
print(ds)
print("Subtract two Series:")
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)

Sample Output:

Add two Series:                                                        
0     3                                                                
1     7                                                                
2    11                                                                
3    15                                                                
4    19                                                                
dtype: int64                                                           
Subtract two Series:                                                   
0    1                                                                 
1    1                                                                 
2    1                                                                 
3    1                                                                 
4    1                                                                 
dtype: int64                                                           
Multiply two Series:                                                   
0     2                                                                
1    12                                                                
2    30                                                                
3    56         
4    90                                                                
dtype: int64                                                           
Divide Series1 by Series2:                                             
0    2.000000                                                          
1    1.333333                                                          
2    1.200000                                                          
3    1.142857                                                          
4    1.111111                                                          
dtype: float64                                                         

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to convert a Panda module Series to Python list and it’s type.
Next: Write a Pandas program to compare the elements of the two Pandas Series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Membership Testing in a Collection

>>> a = ('one', 'two', 'three', 'four', 'five')
>>> if 'one' in a:
...     print('The tuple contains one.')
... 
The tuple contains one.
>>> b = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
>>> if 2 in b.keys():
...     print('The dict has the key of 2.')
... 
The dict has the key of 2.