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
No comments:
Post a Comment