- API reference
- Series
- pandas.Serie...
pandas.Series.transform#
- Series.transform(func,axis=0,*args,**kwargs)[source]#
Call
func
on self producing a Series with the same axis shape as self.- Parameters:
- funcfunction, str, list-like or dict-like
Function to use for transforming the data. If a function, must eitherwork when passed a Series or when passed to Series.apply. If funcis both list-like and dict-like, dict-like behavior takes precedence.
Accepted combinations are:
function
string function name
list-like of functions and/or function names, e.g.
[np.exp,'sqrt']
dict-like of axis labels -> functions, function names or list-like of such.
- axis{0 or ‘index’}
Unused. Parameter needed for compatibility with DataFrame.
- *args
Positional arguments to pass tofunc.
- **kwargs
Keyword arguments to pass tofunc.
- Returns:
- Series
A Series that must have the same length as self.
- Raises:
- ValueErrorIf the returned Series has a different length than self.
See also
Series.agg
Only perform aggregating type operations.
Series.apply
Invoke function on a Series.
Notes
Functions that mutate the passed object can produce unexpectedbehavior or errors and are not supported. SeeMutating with User Defined Function (UDF) methodsfor more details.
Examples
>>>df=pd.DataFrame({'A':range(3),'B':range(1,4)})>>>df A B0 0 11 1 22 2 3>>>df.transform(lambdax:x+1) A B0 1 21 2 32 3 4
Even though the resulting Series must have the same length as theinput Series, it is possible to provide several input functions:
>>>s=pd.Series(range(3))>>>s0 01 12 2dtype: int64>>>s.transform([np.sqrt,np.exp]) sqrt exp0 0.000000 1.0000001 1.000000 2.7182822 1.414214 7.389056
You can call transform on a GroupBy object:
>>>df=pd.DataFrame({..."Date":[..."2015-05-08","2015-05-07","2015-05-06","2015-05-05",..."2015-05-08","2015-05-07","2015-05-06","2015-05-05"],..."Data":[5,8,6,1,50,100,60,120],...})>>>df Date Data0 2015-05-08 51 2015-05-07 82 2015-05-06 63 2015-05-05 14 2015-05-08 505 2015-05-07 1006 2015-05-06 607 2015-05-05 120>>>df.groupby('Date')['Data'].transform('sum')0 551 1082 663 1214 555 1086 667 121Name: Data, dtype: int64
>>>df=pd.DataFrame({..."c":[1,1,1,2,2,2,2],..."type":["m","n","o","m","m","n","n"]...})>>>df c type0 1 m1 1 n2 1 o3 2 m4 2 m5 2 n6 2 n>>>df['size']=df.groupby('c')['type'].transform(len)>>>df c type size0 1 m 31 1 n 32 1 o 33 2 m 44 2 m 45 2 n 46 2 n 4