I have the following df:
| idx | value |
|---|---|
| 0 | 1 |
| 1 | 2 |
| 2 | 3 |
I want to create a new column based on a looking up the value for a given row in conjunction with a constant value in the following matrix:
| x | 1 | 2 | 3 |
|---|---|---|---|
| 1 | 1 | 0.3 | 0.4 |
| 2 | 0.3 | 1 | 0.2 |
| 3 | 0.4 | 0.2 | 1 |
So if constant == 1 then the resulting dataframe would look like:
| idx | value | lookup |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 0.3 |
| 2 | 3 | 0.4 |
Existing answers I've seen reference:
pandas.lookupwhich is deprecatednumpy.selectornumpy.wherewhich aren't really scalable if the lookup matrix gets large
I think there might be a solution around pandas.merge but I'm struggling to get my head around it. Would anyone have any suggestions?
I should probably mention that speed is really important so a vectorised solution would be preferable.
To replicate:
df = pd.DataFrame({"value": [1, 2, 3]})
df_lookup = pd.DataFrame(
index=[1, 2, 3],
data={1: [1, 0.3, 0.4], 2: [0.3, 1, 0.2], 3: [0.4, 0.2, 0.1]}
)