w3resource logo

:

Python Exercises

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

Python Pandas: Date Series Exercise-3 with Solution

Write a Python 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:

Improve this sample solution and post your code through Disqus