繁体   English   中英

无法将自定义 function 二重唱应用到“DataFrame”对象是可变的,因此它们不能被散列

[英]Cannot apply custom function duet to 'DataFrame' objects are mutable, thus they cannot be hashed

我想这样申请 function

def create_output_column(myDf):
    res = myDf.transpose()    
    return res

df.groupby('id').apply(create_output_column(df))

我不知道为什么我收到错误

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

当然,我知道我可以更轻松地进行转置,但这只是更复杂的 function 的示例,但即使在这个简单的示例中,我也会遇到错误。 谁能在这里帮助我? 我知道 dataframe 不能打赌,但我什至看不到它在哪里被散列。

import pandas as pd
d = {'id': [1, 2, 1], 'col2': [3, 4, 5]}
df = pd.DataFrame(data=d)
df

样品 DataFrame

    id  col2
0   1   3
1   2   4
2   1   5

GroupBy(我对组应用了最低限度)

df2 = df.groupby('id')['id','col2'].min()
df2

中间数据

   id   col2
id      
1   1   3
2   2   4

应用定制 Function

df = create_output_column(df2)
df

结果:

id      1   2
_______________
id      1   2
col2    3   4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM