Accelerate Pandas apply function

For a relatively large Pandas DataFrame (a few rows of 100k), I would like to create a series that is the result of applying the function. The problem is that the function is not very fast, and I was hoping that it could be somehow accelerated.

df = pd.DataFrame({
 'value-1': [1, 2, 3, 4, 5],
 'value-2': [0.1, 0.2, 0.3, 0.4, 0.5],
 'value-3': somenumbers...,
 'value-4': more numbers...,
 'choice-index': [1, 1, np.nan, 2, 1]
})

def func(row):
  i = row['choice-index']
  return np.nan if math.isnan(i) else row['value-%d' % i]

df['value'] = df.apply(func, axis=1, reduce=True)

# expected value = [1, 2, np.nan, 0.4, 5]

Any suggestions are welcome.

Update

Very little acceleration (~ 1.1) can be achieved by pre-caching selected columns. funcwill change to:

cached_columns = [None, 'value-1', 'value-2', 'value-3', 'value-4']
def func(row):
  i = row['choice-index']
  return np.nan if math.isnan(i) else row[cached_columns[i]]

But I was hoping for more acceleration ...

+4
source share
1 answer

I think I got a good solution (acceleration ~ 150).

The trick is not to use apply, but to make smart options.

choice_indices = [1, 2, 3, 4]
for idx in choice_indices:
  mask = df['choice-index'] == idx
  result_column = 'value-%d' % (idx)
  df.loc[mask, 'value'] = df.loc[mask, result_column]
+4
source

All Articles