Python Pandas: Selecting Value from row when two values in that row match a value farther up the column -
title little convoluted, help. want retrieve value of values
when variablea
== variableb
== variableb
of current row. example, first row, result
54
because time conditions met in row 3. however, if variablea
== variableb
in current row, result 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 consideration 0
result when variablea
, variableb
match in current row, attempt:
vars = df[['variablea', 'variableb']].values doublematch = (vars[:, none] == vars[none, :] == vars[:, [0]]).all(-1) df['result'] = df['values'].values @ doublematch #python3
but didn't work. thanks!
your example data inconsistent there no row 5 in upper dataframe , bottom dataframe has row index 4 change variableb 2. nonetheless, here solution based on using join
, taking last row of duplicate matches.
here data using - added row result dataframe.
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 s = df[['variablea', 'values']].set_index('variablea').squeeze() s.rename('result', inplace=true) df_final = df.join(s, on='variableb') df_final.loc[df_final['variablea'] == df_final['variableb'], 'result'] = 0 df_final = df_final.reset_index().drop_duplicates('index', keep='last').set_index('index') values variablea variableb result index 0 134 1 3 54.0 1 12 2 6 37.0 2 43 1 2 16.0 3 54 3 3 0.0 4 16 2 7 nan 5 37 6 6 0.0
Comments
Post a Comment