1. Filtering Values in a Series
You can filter values based on conditions.
# Get values greater than 20
print(data[data > 20])
Output:
c 30
d 40
dtype: int64
---
2. Performing Mathematical Operations
You can apply mathematical operations on a Series.
# Multiply all values by 2
print(data * 2)
Output:
a 20
b 40
c 60
d 80
dtype: int64
---
3. Applying Functions Using apply()
You can apply custom functions to modify values.
print(data.apply(lambda x: x ** 2)) # Square each value
Output:
a 100
b 400
c 900
d 1600
dtype: int64
---
4. Checking for Missing (NaN) Values
data_with_nan = pd.Series([10, 20, None, 40], index=['a', 'b', 'c', 'd'])
# Check for missing values
print(data_with_nan.isna())
Output:
a False
b False
c True
d False
dtype: bool
To fill missing values:
print(data_with_nan.fillna(0)) # Replace NaN with 0
---
5. Using map() for Element-wise Mapping
# Convert values to strings with a prefix
print(data.map(lambda x: f"Value: {x}"))
Output:
a Value: 10
b Value: 20
c Value: 30
d Value: 40
dtype: object
---
6. Vectorized Operations (Element-wise)
You can perform vectorized operations efficiently.
# Log transform (requires numpy)
import numpy as np
print(np.log(data))
---
7. Sorting a Series
# Sort by values
print(data.sort_values(ascending=False))
# Sort by index
print(data.sort_index())
---
8. Checking for Membership
print('b' in data) # Output: True
---
9. Converting Series to Other Data Types
# Convert to a list
print(data.tolist())
# Convert to a dictionary
print(data.to_dict())