Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get maximum and minimum of a list in column?

Given that, I have a dataframe as below:

import pandas as pd
import numpy as np

dict = {
        "A": [[1,2,3,4],[3],[2,8,4],[5,8]]
}

dt = pd.DataFrame(dict)

I wish to have the Maximum and minimum of each row in column B. My favorite output is:

              A    B
0  [1, 2, 3, 4]    [1,4]
1           [3]    [3,3] 
2     [2, 8, 4]    [2,8] 
3        [5, 8]    [5,8]

What I already tried is the below code which does not work:

dt["B"] =[np.min(dt.A), np.max(dt.A)]
like image 758
Jeff Avatar asked Jun 03 '20 10:06

Jeff


3 Answers

Like this:

In [1592]: dt['B'] = dt.A.apply(lambda x: [min(x), max(x)])     
In [1593]: dt                                   
Out[1593]: 
              A       B
0  [1, 2, 3, 4]  [1, 4]
1           [3]  [3, 3]
2     [2, 8, 4]  [2, 8]
3        [5, 8]  [5, 8]

As suggested by @Ch3steR, using map since it's faster:

dt['B'] = dt.A.map(lambda x: [min(x), max(x)]) 
like image 118
Mayank Porwal Avatar answered Oct 05 '22 23:10

Mayank Porwal


You can create DataFrame, then minimal and maximal values by DataFrame.agg, convert to lists and assign back if requirement is no loops (Apply are loops under the hood):

df = pd.DataFrame(dt.A.tolist())
dt['B'] = df.agg(['min','max'], axis=1).astype(int).values.tolist()
print (dt)
              A       B
0  [1, 2, 3, 4]  [1, 4]
1           [3]  [3, 3]
2     [2, 8, 4]  [2, 8]
3        [5, 8]  [5, 8]

If no problem with loops another solution with list comprehension, it should be faster like apply, depends of real data:

dt['B'] =  [[min(x), max(x)] for x in dt.A]
like image 23
jezrael Avatar answered Oct 05 '22 23:10

jezrael


Just an alternative with explode:

dt['B'] = (dt['A'].explode().astype(int).groupby(level=0).agg(['min','max'])
           .to_numpy().tolist())
print(dt)

              A       B
0  [1, 2, 3, 4]  [1, 4]
1           [3]  [3, 3]
2     [2, 8, 4]  [2, 8]
3        [5, 8]  [5, 8]
like image 25
anky Avatar answered Oct 05 '22 22:10

anky