Wednesday, August 27, 2025

๐ŸSeries operations and methods in pandas

A Pandas Series supports many operations that allow you to quickly analyze and manipulate data. These include arithmetic operations, vectorized computations, and a wide range of built-in methods.

๐Ÿ”น Arithmetic Operations

You can perform element-wise arithmetic directly on a Series, similar to NumPy arrays.


import pandas as pd

s = pd.Series([10, 20, 30, 40])

print(s + 5)    # Add 5 to each element
print(s * 2)    # Multiply each element by 2
print(s / 10)   # Divide each element by 10
    

๐Ÿ‘‰ Output:


0    15
1    25
2    35
3    45
dtype: int64

0    20
1    40
2    60
3    80
dtype: int64

0    1.0
1    2.0
2    3.0
3    4.0
dtype: float64
    

๐Ÿ”น Vectorized Computations

Series operations are vectorized, meaning they are applied to the entire array without explicit loops. This makes them fast and efficient.


print(s ** 2)   # Square of each element
print(s % 3)    # Modulus with 3
    

๐Ÿ‘‰ Output:


0    100
1    400
2    900
3   1600
dtype: int64

0    1
1    2
2    0
3    1
dtype: int64
    

๐Ÿ”น Common Built-in Methods

Pandas Series has several handy methods to summarize and transform data.


print(s.sum())      # Sum of all elements
print(s.mean())     # Average value
print(s.max())      # Maximum
print(s.min())      # Minimum
print(s.describe()) # Summary statistics
    

๐Ÿ‘‰ Example Output:


100
25.0
40
10
count     4.000000
mean     25.000000
std      12.909944
min      10.000000
25%      17.500000
50%      25.000000
75%      32.500000
max      40.000000
dtype: float64
    

๐Ÿ–ฅ️ Practice in Browser

No comments:

Post a Comment

๐ŸWhat is scikitlearn??