Search code examples
pythonpandaslistminimum

Extract data from a list in python


I have the following list:

[N               12.000000
 mean             2.011608
 median           2.021611
 std              0.572034
 relative std     0.284350
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.571815
 relative std     0.284262
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.572101
 relative std     0.284412
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.572115
 relative std     0.284440
 dtype: float64,
 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.571872
 relative std     0.284313
 dtype: float64]

I would like to extract the data (N, mean, std, relative std) from the list which has the minimum relative std value. The output of the above list should be the following:

 N               12.000000
 mean             2.011608
 median           2.021611
 std              0.571815
 relative std     0.284262
 dtype: float64

What I tried so far?

min(list)

But the throws the following error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


Solution

  • Use min with lambda function for select values of Series by label std:

    s1 = pd.Series([0,1,2], index=['std','min','max'])
    s2 = pd.Series([4,1,2], index=['std','min','max'])
    s3 = pd.Series([0.8,1,2], index=['std','min','max'])
    
    L = [s1, s2, s3]
    
    s = min(L, key=lambda x:x.loc['std'])
    print (s)
    std    0
    min    1
    max    2
    dtype: int64
    

    For test all minimals:

    print ([x.loc['std'] for x in L])
    [0, 4, 0.8]
    

    And for index use np.argmin:

    import numpy as np
    print (np.argmin([x.loc['std'] for x in L]))
    0