Search code examples
pandassummutable

'Series' objects are mutable, thus they cannot be hashed trying to sum columns and datatype is float


I am tryning to sum all values in a range of columns from the third to last of several thousand columns using:

day3prep['D3counts'] = day3prep.sum(day3prep.iloc[:, 2:].sum(axis=1))

dataframe is formated as:

ID G1  Z1  Z2 ...ZN
0  50  13  12 ...62
1  51  62  23 ...19

dataframe with summed column:
ID G1  Z1  Z2 ...ZN D3counts
0  50  13  12 ...62 sum(Z1:ZN in row 0)
1  51  62  23 ...19 sum(Z1:ZN in row 1)

I've changed the NaNs to 0's. The datatype is float but I am getting the error:

'Series' objects are mutable, thus they cannot be hashed

Solution

  • You only need this part:

    day3prep['D3counts'] = day3prep.iloc[:, 2:].sum(axis=1)
    

    With some random numbers:

    import pandas as pd
    import random
    
    random.seed(42)
    day3prep = pd.DataFrame({'ID': random.sample(range(10), 5), 'G1': random.sample(range(10), 5),
        'Z1': random.sample(range(10), 5), 'Z2': random.sample(range(10), 5), 'Z3': random.sample(range(10), 5)})
    
    day3prep['D3counts'] = day3prep.iloc[:, 2:].sum(axis=1)
    

    Output:

    > day3prep
    
    
        ID  G1  Z1  Z2  Z3  D3counts
    0   1   2   0   8   8        16
    1   0   1   9   0   6        15
    2   4   8   1   3   3         7
    3   9   4   7   5   7        19
    4   6   3   6   6   4        16