Title is a little convoluted, but hopefully this will help. I want to retrieve the value of values when variableA == variableB == variableB of the current row. For example, for the first row, result will be 54 because the only time those conditions are met are in row 3. However, if variableA == variableB in the current row, the result will be 0. Example Data:
values variableA variableB
0 134 1 3
1 12 2 6
2 43 1 2
3 54 3 3
4 16 2 7
5 37 6 6
Desired Result:
values variableA variableB result
0 134 1 3 54
1 12 2 6 37
2 43 1 2 16
3 54 3 3 0
4 16 2 7 NaN
5 37 6 6 0
Not taking into consideration the 0 result when variableA and variableB match in the current row, my attempt:
vars = df[['variableA', 'variableB']].values
doublematch = (vars[:, None] == vars[None, :] == vars[:, [0]]).all(-1)
df['result'] = df['values'].values @ doublematch #python3
but that clearly didn't work. Thanks!
variableBandvariableAoccur more than twice. However, there should only be one instance where a row has the same variable under both columns (as in,variableA == variableBfor that row). The columns were generated from a frozenset tuple representing every unique tuple from a list N elements long.