pandas Series Operations
Perform arithmetic operations and use various Series methods.
pandas Series Operations and Methods
Learn to perform arithmetic operations on pandas Series and also learn about different pandas Series methods.
Arithmetic operations
You can perform various arithmetic operations on pandas Series similar to that of Python List or NumPy arrays.
# Importing the pandas library as pd
import pandas as pd
# Importing the NumPy library as np
import numpy as np
# Initializing a Python list
lst = [100, 200, 300, 400]
# Creating a pandas Series from a Python List
s = pd.Series(lst)
# Printing the Series
print(s)Arithmetic operations between a pandas Series and a scalar value
When performing an arithmetic operation between a pandas Series and a scalar value, the operation is broadcasted across all the data values of a pandas Series.
# Addition
print(s+100)
# Subtraction
print(s-100)
# Multiplication
print(s*100)
# Division
print(s/100)You can also perform various arithmetic operations using functions in Python and it will be broadcasted to all of the data values in the pandas Series.
# Finding the log of the values
print(np.log(s))
# Finding the exponential of the values
print(np.exp(s))Arithmetic operations between two pandas Series
Arithmetic operations between two pandas Series is equivalent to performing vector arithmetics.
# Printing the Series
print(s)
# Addition
print(s+s)
# Subtraction
print(s-s)
# Multiplication
print(s*s)
# Division
print(s/s)pandas Series Methods
A pandas Series method is a function that can be called from the pandas Series itself. The following table shows a list of commonly used pandas Series methods along with their meaning.
Methods of a pandas Series | Definition |
|---|---|
| abs() | Return a Series/DataFrame with absolute numeric value of each element. |
| agg() | Aggregate using one or more operations over the specified axis. |
| argmax() | Return int position of the largest value in the Series. |
| argmin() | Return int position of the smallest value in the Series. |
| apply() | Invoke function on values of Series. |
| describe() | Generate descriptive statistics. |
| fillna() | Fill NA/NaN values using the specified method. |
| isnull() | Detect missing values. |
| sum() | Return the sum of the values over the requested axis. |
# Initializing a Python list
lst = [-100, None, 300, 400]
# Creating a pandas Series from a Python List
s = pd.Series(lst)
# Printing the Series
print(s)
# Returns a Series/DataFrame with absolute numeric value of each element
print(s.abs())
# Aggregate using one or more operations
print(s.agg('min'))
print(s.agg(['min', 'max', 'mean', 'median']))
# Return int position of the largest value in the Series
print(s.argmax())
# Return int position of the smallest value in the Series
print(s.argmin())
# Invoke function on values of Series
print(s.apply(lambda x:x+1))
# Generate descriptive statistics
print(s.describe())
# Fill NA/NaN values
print(s.fillna(0))
# Detect missing values
print(s.isnull())
# Print the sum of null values present
print(s.isnull().sum())