Pandas

3.Pandas Series: What are Operations on Pandas Series?

What are Operations on Pandas Series?

Pandas Series supports various operations like arithmetic, statistical functions, and element-wise operations. These operations allow easy data manipulation and analysis.

Operations can be categorized into:
Arithmetic Operations (Addition, Subtraction, Multiplication, etc.)
Statistical Functions (Mean, Median, Min, Max, etc.)
Element-wise Operations (Applying functions on Series)


1. Arithmetic Operations on Series

Pandas Series supports element-wise arithmetic operations just like NumPy arrays.

Example 1: Performing Arithmetic Operations

import pandas as pd  

# Creating two Series  
s1 = pd.Series([10, 20, 30, 40])  
s2 = pd.Series([1, 2, 3, 4])  

# Arithmetic operations  
print(s1 + s2)  # Addition  
print(s1 - s2)  # Subtraction  
print(s1 * s2)  # Multiplication  
print(s1 / s2)  # Division  

Output:

0    11  
1    22  
2    33  
3    44  
dtype: int64  

0     9  
1    18  
2    27  
3    36  
dtype: int64  

0    10  
1    40  
2    90  
3   160  
dtype: int64  

0    10.0  
1    10.0  
2    10.0  
3    10.0  
dtype: float64  

🔹 Arithmetic operations are applied element-wise. If index values do not match, Pandas fills missing values with NaN.


2. Statistical Operations on Series

Pandas provides built-in functions to perform statistical calculations.

Example 2: Applying Statistical Functions

import pandas as pd  

# Creating a Series  
data = pd.Series([5, 10, 15, 20, 25])  

# Statistical operations  
print("Sum:", data.sum())  
print("Mean:", data.mean())  
print("Median:", data.median())  
print("Max:", data.max())  
print("Min:", data.min())  

Output:

Sum: 75  
Mean: 15.0  
Median: 15.0  
Max: 25  
Min: 5  

🔹 These functions help in quick aggregation of numerical data.


3. Element-wise Operations on Series

We can apply custom functions to each element of a Series using .apply().

Example 3: Applying a Custom Function

import pandas as pd  

# Creating a Series  
data = pd.Series([2, 4, 6, 8, 10])  

# Applying a function to square each element  
squared_data = data.apply(lambda x: x ** 2)  

print(squared_data)

Output:

0      4  
1     16  
2     36  
3     64  
4    100  
dtype: int64  

🔹 .apply() is useful for complex transformations on each element.


Key Takeaways:

Arithmetic operations are applied element-wise between Series.
Statistical functions like sum(), mean(), min(), and max() help analyze data.
Element-wise functions can be applied using .apply().

Leave a Reply

Your email address will not be published. Required fields are marked *