0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I voted for your 2nd answer for readability.
1

An alternative with mask:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.