transform method of pandas can not pass multi methods

transform method of pandas can not pass multi methods



I want to pass 2 methods to transform method of pandas as the API says it can pass a list of functions or dict of column names -> functions. I pass a list of functions, but it does not work:


transform


import pandas as pd
import numpy as np
df = pd.DataFrame('rti':['a','a','b','c','b','c','a'],'ts':[10,10,9,12,9,13,11],'rs':[8,8,22,11,12,11,9])
df.groupby('rti').transform(['mean','sum'])



It shows:"TypeError: unhashable type: 'list'"






Which version of pandas? Perhaps it is a bug?

– Alexander
Sep 18 '18 at 2:03






the version is 0.23.4

– littlely
Sep 18 '18 at 2:08




2 Answers
2



It works, but the functions used must not be aggregation functions such as sum, max or min.


>>> df.transform([np.abs, np.sign])
rs ts
absolute sign absolute sign
0 8 1 10 1
1 8 1 10 1
2 22 1 9 1
3 11 1 12 1
4 12 1 9 1
5 11 1 13 1
6 9 1 11 1



Refer to the documentation here. Note that the transform method for groupby objects accepts only a function (not a list of functions which is for the dataframe transform method).


transform


groupby



Per the doc string of the tranform method of groupby objects:


tranform


groupby



Signature: gb.transform(func, *args, **kwargs)



Docstring:
Call function producing a like-indexed DataFrame on each group and
return a DataFrame having the same indexes as the original object
filled with the transformed values



f : function
Function to apply to each group



Each group is endowed the attribute 'name' in case you need to know
which group you are working on.



The current implementation imposes three requirements on f:






I try this before , would you like test after groupby``df.groupby('rti').transform([np.abs, np.sign])

– Wen-Ben
Sep 18 '18 at 2:11


groupby``df.groupby('rti').transform([np.abs, np.sign])






I think OP is after the transform method on .DataFrameGroupBy objects, not on DataFrames.

– DSM
Sep 18 '18 at 2:11



Seems like transform do not accept list of function , Open issue in github


transform


df.groupby('rti').agg(['mean','sum']).reindex(df.rti)
Out[12]:
rs ts
mean sum mean sum
rti
a 8.333333 25 10.333333 31
a 8.333333 25 10.333333 31
b 17.000000 34 9.000000 18
c 11.000000 22 12.500000 25
b 17.000000 34 9.000000 18
c 11.000000 22 12.500000 25
a 8.333333 25 10.333333 31



Thanks for contributing an answer to Stack Overflow!



But avoid …



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

.name

Greenpeace

Thomas Howard, 21st Earl of Arundel