Home > Enterprise >  Selecting values from column according to a specific value in different column
Selecting values from column according to a specific value in different column

Time:11-06

I am kinda stuck on selecting values from one column according to a specific value form a different column. For example, the following dataframe:

 name   profit 

Anna    10000
Alice   5000 
Anna    500
Anna    4000
Jack    2000

I am trying to get the mean of Anna's profit values from the profit column. I tried using df['name'].str.contains('Anna') to select Anna from the name column, however, I'm not sure how I can go about selecting the profit values where it's only Anna.

CodePudding user response:

You can use query:

df.query('name == "Anna"')['profit'].mean()

Or eq and slicing:

df.loc[df['name'].eq('Anna'), 'profit'].mean()

variant:

df[df['name'].eq('Anna')]['profit'].mean()

output: 4833.33

CodePudding user response:

An alternative with mask:

>>> df.mask(~df['name'].str.contains('Anna'))['profit'].mean()
4833.333333333333
  • Related