I have a python pandas dataframe with multiple columns. One of the columns has a value that matters. Which column matters changes for each row. So I decided to add a column that gives the value that matters. By using numpy.digitize it is fast to find which column to use for me. I store the array in a new column, let's call it 'assigned columns column'. But I struggle to find a time efficient way to store the actual value into a new column.
I tried
data['name'] = data.apply(lambda row: row[row['assigned columns column']], axis=1)
but it is very slow with my number of rows.
value_that_matters = [data.loc[i,strip] for i, strip in enumerate(expected_strips) ]
data['name'] = value_that_matters
was much quicker with expected_strips being the numpy array returned by numpy.digtize and content of the 'assigned columns column'. But there must be a quicker, better way to grab specific column's value for each row... Please tell me :)