saikit
saikit

Reputation: 1

Selecting values from column according to a specific value in different column

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.

Upvotes: 0

Views: 45

Answers (2)

Corralien
Corralien

Reputation: 120559

An alternative with mask:

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

Upvotes: 1

mozway
mozway

Reputation: 262484

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

Upvotes: 1

Related Questions