[英]Python Pandas: "Series" objects are mutable, thus cannot be hashed when using .groupby
I want to take the 2nd derivative of column['Value'] and place it into another column.我想取 column['Value'] 的二阶导数并将其放入另一列。 There is also another column called ['Cycle'] that organizes the data into various cycles.
还有另一列称为 ['Cycle'] 将数据组织成各种循环。 So for each cycle, I want to take the 2nd derivative of those sets of number.
所以对于每个周期,我想对这些数字集进行二阶导数。
I have tried using this:我试过用这个:
Data3['Diff2'] = Data3.groupby('Cycle#').apply(Data3['Value'] - 2*Data3['Value'].shift(1) + Data3['Value'].shift(2))
Which works for giving me the 2nd derivative (before adding the groupby) but now I am getting the error: TypeError: 'Series' objects are mutable, thus they cannot be hashed哪个适用于给我二阶导数(在添加 groupby 之前),但现在我收到错误:TypeError: 'Series' objects are mutable, 因此它们不能被散列
Anyone know why?有谁知道为什么?
rng = np.random.default_rng(seed=42)
df = pd.DataFrame(
{"Cycle#": rng.integers(1,4, size=12),
"Value": rng.integers(1,11, size=12)*10
})
df
###
Cycle# Value
0 1 80
1 3 80
2 2 80
3 2 80
4 2 60
5 3 20
6 1 90
7 3 50
8 1 60
9 1 40
10 2 20
11 3 100
df['Diff2'] = df.groupby('Cycle#', as_index=False)['Value'].transform(lambda x:x - 2*x.shift(1) + x.shift(2))
df
###
Cycle# Value Diff2
0 1 80 NaN
1 3 80 NaN
2 2 80 NaN
3 2 80 NaN
4 2 60 -20.0
5 3 20 NaN
6 1 90 NaN
7 3 50 90.0
8 1 60 -40.0
9 1 40 10.0
10 2 20 -20.0
11 3 100 20.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.