简体   繁体   English

Python Pandas:“系列”对象是可变的,因此在使用时不能散列。groupby

[英]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.

相关问题 系列对象是可变的,因此它们不能在 Python pandas 数据帧上散列 - Series objects are mutable, thus they cannot be hashed on Python pandas dataframe Python 和 Pandas:“系列”对象是可变的,因此它们不能被散列 - Python & Pandas: 'Series' objects are mutable, thus they cannot be hashed pandas Python Series对象是可变的,因此它们不能在查询方法中进行散列 - pandas Python Series objects are mutable, thus they cannot be hashed in query method Python:“系列”对象是可变的,因此它们不能被散列 - Python : 'Series' objects are mutable, thus they cannot be hashed Pandas loc 错误:“系列”对象是可变的,因此它们不能被散列 - Pandas loc error: 'Series' objects are mutable, thus they cannot be hashed Pandas 返回错误:“系列”对象是可变的,因此它们不能被散列 - Pandas returns error: 'Series' objects are mutable, thus they cannot be hashed 在pandas系列上使用apply方法获取TypeError'Series'对象是可变的,因此不能将它们散列 - Using apply method on pandas series getting TypeError 'Series' objects are mutable, thus they cannot be hashed 类型错误:“系列”对象是可变的,因此它们不能被散列 - TypeError: 'Series' objects are mutable, thus they cannot be hashed “系列”对象是可变的,因此它们不能被散列 - 'Series' objects are mutable, thus they cannot be hashed 当我在dataframe(pandas)中设置值时出现错误:“系列”对象是可变的,因此无法进行哈希处理 - when I set value in dataframe(pandas) there is error: 'Series' objects are mutable, thus they cannot be hashed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM