Home > Software engineering >  Python Pandas: "Series" objects are mutable, thus cannot be hashed when using .groupby
Python Pandas: "Series" objects are mutable, thus cannot be hashed when using .groupby

Time:09-02

I want to take the 2nd derivative of column['Value'] and place it into another column. There is also another column called ['Cycle'] that organizes the data into various cycles. 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

Anyone know why?

CodePudding user response:

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
  • Related