I would like to get a specific column value when the other 2 columns values are equal, how can I accomplish this?
user | center | right |
---|---|---|
mary | 1000 | 1000 |
jane | 9999 | 2222 |
Let's say the column value that I want to get is 'user'. Expected output is
mary
CodePudding user response:
Another option is using DataFrame.query
, although it's not quite its typical use case.
>>> df.query('center == right')['user'].iat[0]
'mary'
CodePudding user response:
df[df['center'] == df['right']]['user'][0]
'mary '
OR
df[df['center'] == df['right']]
user center right
0 mary 1000 1000
CodePudding user response:
I think @Naveed's answer works perfectly. Another way I would suggest solving the problem is with the '.loc' method.
I would write the code like this:
# Defining the filtering condition
fc1 = df['center'] == df['right']
# Applying the filter condition to obtain the value of the desired column
df.loc[fc1, 'user'].values[0]
Result:
'mary'
Hope it helps!
CodePudding user response:
df.loc[df['center']==df['right'], 'user'].iloc[0]
'mary'