Skip to content
Advertisement

Is it possible access a list stored in a dataframe in a vectorized manner?

Considering a dataframe like so:

JavaScript
JavaScript

I want to create a new column ‘extracted_value’ which would be the value contained in the list at ‘indexes’ index (list = [0, 1, 2], indexes = 0 -> 0, indexes = 1 -> 1, and so on)

JavaScript

Doing it with iterrows() is extremely slow as I work with dataframes containing multiple millions of lines.

I have tried the following:

JavaScript

But it results in:

JavaScript

The following will just results in extracted_value containing the whole list:

JavaScript

Thank you for your help.

Advertisement

Answer

What you tried was almost ok, you only needed to put it into pd.DataFrame.apply while setting axis argument as 1 to make sure the function is applied on each row:

JavaScript
Advertisement