Skip to content Skip to sidebar Skip to footer

Largest Element All Lists In Panda Series

I have a pandas series say import pandas as pd a = pd.Series([ [1, 2, 3, 4, 5], [6, 7, 8, 3, 334], [333, 4, 5, 3, 4] ]) I want to find the largest element in all lists

Solution 1:

Option 1 Only works if elements are actually list. This is because sum concatenates lists. This is also likely very slow.

max(a.sum())

334

Option 2 minimal two tiered application of max

max(map(max, a))

334

Option 3 Only works if all lists are same length

np.max(a.tolist())

334

Option 4 One application of max on an unwound generator

max(x forlin a forxin l)

334

Solution 2:

To dataframe

pd.DataFrame(a.values.tolist()).max().max()
Out[200]: 334

Or numpy.concatenate

np.concatenate(a.values).max()
Out[201]: 334

Or

max(sum(a,[]))
Out[205]: 334

Solution 3:

This is one way:

max(max(i) for i in a)

Functional variant:

max(map(max, a))

Alternative method which only calculates one max:

from toolz import concatmax(concat(a))

For the fun of it below is some benchmarking. The lazy function concat and optimised map / list comprehension do best, then come numpy functions, pandas methods usually worse, clever sum applications last.

import numpy as np
from toolz import concat
import pandas as pd

a = pd.Series([list(np.random.randint(0, 10, 100)) for i inrange(1000)])

# times in ms5.92max(concat(a))
6.29max(map(max, a))
6.67max(max(i) for i in a)
17.4max(x for l in a for x in l)
19.2  np.max(a.tolist())
20.4  np.concatenate(a.values).max()
64.6  pd.DataFrame(a.values.tolist()).max().max()
373   np.max(a.apply(pd.Series).values)
672max(sum(a,[]))
696max(a.sum())

Solution 4:

Yet another answer using np.max:

import numpy as np
np.max(a.apply(pd.Series).values)
Out[175]: 334

Post a Comment for "Largest Element All Lists In Panda Series"