Gilberto
Gilberto

Reputation: 863

How to get the minimum value of a row list in a pandas dataframe

I have a pandas dataframe with a column made of lists.
The goal is to find the min of every list in row (in an efficient way).

E.g.

import pandas as pd
df = pd.DataFrame(columns=['Lists', 'Min'])
df['Lists'] = [ [1,2,3], [4,5,6], [7,8,9] ]
print(df)

The goal is the Min column:

       Lists  Min
0  [1, 2, 3]  1
1  [4, 5, 6]  4
2  [7, 8, 9]  7

Thank you in advance,
gil

Upvotes: 5

Views: 7736

Answers (1)

jezrael
jezrael

Reputation: 863176

You can use apply with min:

df['Min'] = df.Lists.apply(lambda x: min(x))
print (df)
       Lists  Min
0  [1, 2, 3]    1
1  [4, 5, 6]    4
2  [7, 8, 9]    7

Thank you juanpa.arrivillaga for idea:

df['Min'] = [min(x) for x in df.Lists.tolist()]
print (df)
       Lists  Min
0  [1, 2, 3]    1
1  [4, 5, 6]    4
2  [7, 8, 9]    7

Timings:

##[300000 rows x 2 columns]
df = pd.concat([df]*100000).reset_index(drop=True)

In [144]: %timeit df['Min1'] = [min(x) for x in df.Lists.values.tolist()]
10 loops, best of 3: 137 ms per loop

In [145]: %timeit df['Min2'] = [min(x) for x in df.Lists.tolist()]
10 loops, best of 3: 142 ms per loop

In [146]: %timeit df['Min3'] = [min(x) for x in df.Lists]
10 loops, best of 3: 139 ms per loop

In [147]: %timeit df['Min4'] = df.Lists.apply(lambda x: min(x))
10 loops, best of 3: 170 ms per loop

Upvotes: 9

Related Questions