0

I have two column, age and income. There are some income values in same age. How do I get the average from specific age?

If I try to use

df.groupby('age')['income'].mean() 

I will get a dataframe, How do I get a value for a specific age?

ndc85430
  • 1,395
  • 3
  • 11
  • 17
BiIs
  • 1
  • 5
    (1) Format the code properly. (2) Add an appropriate tag for the library you are using (pandas, polars, spark,...). – Michael Butscher Aug 20 '23 at 11:02
  • 1
    `df.groupby('age')['income'].mean().loc[age]` https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html – Adam.Er8 Aug 20 '23 at 11:03
  • Are you just trying to do it for one age or do you want to get all of them then go through them one-by-one? Please [edit] and add some example input and your desired output. for reference see [mre] and [How to make good reproducible pandas examples](/q/20109391/4518341). BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want more tips. – wjandrea Aug 20 '23 at 15:28
  • FWIW, you might find your answer in the user guide: [Indexing and selecting data](https://pandas.pydata.org/docs/user_guide/indexing.html) – wjandrea Aug 20 '23 at 15:30

1 Answers1

1

You're right that the groupby method followed by taking the mean will give you a series with the average income for each age in your DataFrame. If you want to get the average income for a specific age, you need to pass the specific age you're interested in.

I assume that you want the average income for an age of 30. You would do:

average_income_age_30 = df.groupby('age')['income'].mean().loc[30]

This will first perform the grouping and mean calculation, and then use the loc method to retrieve the value for age 30.

Or, you can assign the result of the groupby and mean to a variable and then use that:

average_incomes_by_age = df.groupby('age')['income'].mean()
average_income_age_30 = average_incomes_by_age.loc[30]
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60