-
Book Overview & Buying
-
Table Of Contents
Pandas Cookbook - Third Edition
By :
The .agg and .transform methods we have seen so far apply to an entire sequence of values at once. Generally, in pandas, this is a good thing; it allows pandas to perform vectorized operations that are fast and computationally efficient.
Still, sometimes, you as an end user may decide that you want to trade performance for customization or finer-grained control. This is where the .map methods can come into the picture; .map helps you apply functions individually to each element of your pandas object.
Let’s assume we have a pd.Series of data that mixes together both numbers and lists of numbers:
ser = pd.Series([123.45, [100, 113], 142.0, [110, 113, 119]])
ser
0 123.45
1 [100, 113]
2 142.0
3 [110, 113, 119]
dtype: object
.agg or .transform are not suitable here because we do not have a uniform data type – we really have to inspect each element to make a decision on how to handle it.
For...