Python 无法将自定义函数duet应用于';数据帧';对象是可变的,因此不能对其进行散列
我想这样应用一个函数Python 无法将自定义函数duet应用于';数据帧';对象是可变的,因此不能对其进行散列,python,pandas,dataframe,hash,Python,Pandas,Dataframe,Hash,我想这样应用一个函数 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 当然,我知道我可以更容易地转置它,但这只是一个更复杂
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
当然,我知道我可以更容易地转置它,但这只是一个更复杂函数的例子,但即使在这个简单的例子中,我也会得到错误。有人能帮我吗?我知道数据帧不能进行散列,但我甚至不知道它在哪里被散列
import pandas as pd
d = {'id': [1, 2, 1], 'col2': [3, 4, 5]}
df = pd.DataFrame(data=d)
df
示例数据帧
id col2
0 1 3
1 2 4
2 1 5
GroupBy(我对组应用了最小值)
中间数据
id col2
id
1 1 3
2 2 4
应用自定义函数
df = create_output_column(df2)
df
结果:
id 1 2
_______________
id 1 2
col2 3 4
.apply
将函数作为参数,并传递一个数据帧(函数的结果)。此外,传递给的函数应用获取pd.Series
,而不是数据帧。我不确定这里的拼写错误是什么,但你能解释一下这里会发生什么吗?没错,我不理解。但是如果我不使用参数(myDf),我会得到create\u output\u column()缺少1个必需的位置参数:“myDf”。那么,该如何传递该论点呢?问题是,我想将该函数应用于所有分组的数据帧。所以我有10个身份证。
id 1 2
_______________
id 1 2
col2 3 4