Home > Enterprise >  Pandas return value of column with conditional row statemnet on a different column
Pandas return value of column with conditional row statemnet on a different column

Time:10-16

What is an equivalent of the following:

import pandas as pd 
x=pd.DataFrame([['a','a','b','c'],[1,2,3,4]],columns=['lets','nums'])
b_nums=x[x['lets']=='b'].loc['nums']

Should return 3 I can do

y=x[x['lets']=='b']
b_nums=y.loc['nums']

The values I'm searching for are unique however there's got to be a more efficient way

CodePudding user response:

For a single condition:

x[x.lets=='b'].nums.values

For any of multiple conditions:

x[x.lets.isin(["b", "c"])].nums.values
  • Related