Search code examples
pythonpandasdataframedata-scienceseries

Python apply element-wise operation between to series where every element is a list


I have to series, where every element of the series is a list:

s1 = pd.Series([[1,2,3],[4,5,6],[7,8,9]])
s2 = pd.Series([[1,2,3],[1,1,1],[7,8,8]])

And I want to calculate element-wise sklearn.metrics.mean_squared_error, so I will get:

[0, 16.666, 0.33]

What is the best way to do it?


Solution

  • With this setup,

    import pandas as pd
    from sklearn.metrics import mean_squared_error
    
    s1 = pd.Series([[1,2,3],[4,5,6],[7,8,9]])
    s2 = pd.Series([[1,2,3],[1,1,1],[7,8,8]])
    

    here's two options, first with map,

    >>> pd.Series(map(mean_squared_error, s1, s2))
    0     0.000000
    1    16.666667
    2     0.333333
    dtype: float64
    

    and second with np.vectorize:

    >>> import numpy as np
    >>> mean_squared_error_vec = np.vectorize(mean_squared_error)
    >>> mean_squared_error_vec(s1, s2)
    array([ 0.        , 16.66666667,  0.33333333])