I have a pandas dataframe where one column consists of lists:
import pandas as pd
df = pd.DataFrame({"a" : [[1,2,3], [4,5,6], [7,8,9]]})
I want to add, e.g., the first and last index of each list. For that I use apply
:
df['a'].apply(lambda x: x[0] + x[-1])
This acts on each row individually, which can be quite intensive if the dataframe is large. Is there a way to vectorize this operation?