如何在Pandas中快速提取列表值列的元素最小值?
Great question! I totally get why you're frustrated with the slow apply method—it's notoriously inefficient for large datasets because it processes rows one by one instead of leveraging pandas' vectorized superpowers. Let's dive into some way faster alternatives:
1. List Comprehension (Universal Fast Solution)
This is my go-to for most cases, even when lists in column c have varying lengths. List comprehensions are optimized in Python and outperform lambda-based apply by a wide margin:
df['d'] = [min(lst) for lst in df['c']]
2. Numpy Vectorization (Best for Uniform List Lengths)
If every list in column c has the same number of elements, using numpy's vectorized operations will give you the fastest speed possible—since numpy operations run in C under the hood:
import numpy as np # Convert the list column to a 2D numpy array arr = np.array(df['c'].tolist()) # Compute row-wise minimum df['d'] = arr.min(axis=1)
3. Pandas DataFrame Constructor (Alternative for Varying Lengths)
Another solid option for when lists have different lengths is to convert the list column into a DataFrame, then compute the row-wise minimum. This is still way faster than apply:
df['d'] = pd.DataFrame(df['c'].tolist()).min(axis=1)
Why These Are Faster
df.apply(lambda x: min(x['c']))runs a Python-level loop over every row, which is slow for large datasets.- List comprehensions use optimized Python loops, while numpy/pandas methods use C-level vectorization—both avoid the overhead of
apply's row-by-row processing.
Speed Test Example
To see the difference for yourself, use %timeit in Jupyter/IPython:
# Test data with 10,000 rows df_large = pd.DataFrame({'c': [[np.random.randint(0,100) for _ in range(5)] for _ in range(10000)]}) %timeit df_large['d'] = df_large['c'].apply(lambda x: min(x)) %timeit df_large['d'] = [min(lst) for lst in df_large['c']] %timeit df_large['d'] = np.min(np.array(df_large['c'].tolist()), axis=1)
You'll likely see the list comprehension or numpy method run 5-10x faster than the original apply approach.
内容的提问来源于stack exchange,提问作者Gemini

