I want to select one specific element of a column of lists based on another columns cell value in a Pandas Dataframe.
list_column value
0 [a, b, c] 0
1 [a, b, c] 2
2 [a, b, c] 1
so the desired output will be:
list_column value selected
0 [a, b, c] 0 a
1 [a, b, c] 2 c
2 [a, b, c] 1 b
I tried using the
df["selected"] = df['list'].str[df["value"]]
but was not managing to apply this correctly.
CodePudding user response:
I've checked the previous solution doesn't work I've tested this one it works
for index, row in df.iterrows():
value = row['values']
df.loc[index,"selected"] = df.loc[index,'list_column'][value]
CodePudding user response:
For a vectorial approch use numpy slicing:
import numpy as np
df['selected'] = np.vstack(df['list_column'])[np.arange(len(df)), df['value']]
Output:
list_column value selected
0 [a, b, c] 0 a
1 [a, b, c] 2 c
2 [a, b, c] 1 b