Status
Done
TABLE OF CONTENT
1. Element-wise Operations
df['series'].map(arg) → Map values using a dict/Series/function (element-wise)df['series'].apply(func) → Apply a function along the Series (element-wise or aggregate)
2. Aggregation Methods
df['series'].agg(func) → Aggregate using one or multiple operations (alias:Âaggregate())- Common funcs:Â
'sum',Â'mean',Â'min',Â'max', or custom functions
3. Transformation Methods
df['series'].transform(func) → Return transformed values (same shape as input)- Differs fromÂ
agg(): maintains original dimensions
4. Series Combination
df['series'].combine(other, func) → Combine with another Series using a functiondf['series'].combine_first(other) → Fill nulls using another Series
Key Differences:
Method | Best For | Returns |
map() | Simple value replacements | Same length |
apply() | Complex element-wise operations | Same length |
agg() | Summarizing data | Scalar/value |
transform() | Group-aware transformations | Same length |
Example Usage:
s1 = pd.Series([1, 2, None])
s2 = pd.Series([10, None, 30])
# Element-wise
s1.map({1: 'a', 2: 'b'}) # → ['a', 'b', None]
s1.apply(lambda x: x*2 if pd.notnull(x) else 0) # → [2, 4, 0]
# Aggregation
s1.agg(['sum', 'mean']) # → Returns Series with both metrics
# Transformation
s1.transform(lambda x: x - x.mean()) # → Center data
# Combination
s1.combine(s2, max) # → [10, 2, 30]
s1.combine_first(s2) # → [1, 2, 30] (fills nulls)When to Use:
- UseÂ
map()Â for simple value lookups - UseÂ
apply()Â for custom complex operations - UseÂ
agg()Â for summaries,Âtransform()Â for group-wise standardization - UseÂ
combine()Â for element-wise logic between Series
import pandas as pd
import seaborn as sns
# Load Titanic dataset
df = sns.load_dataset('titanic')
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True
Function Mapping & Transformation in Pandas Series
‣
1. Element-wise Operations
‣
2. Aggregation Methods
‣
3. Transformation Methods
‣
4. Series Combination
‣
Performance Considerations
‣