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
pandas
data structures are using theobject
dtype, you are killing efficiency. – Poppdf["b"] =np.array(map(list,[df["a"].shift(x) for x in range(1,4)])).T.tolist()
- see [#37968324. Is there a way to speed up? – KiriDataFrame
, making it of dtypeobject
. The dtype is inherited for the underlying numpy data structure, andobject
dtypes are slow. It's not the algorithm, it's your data structure. – Popp