0
# Import your libraries
import pandas as pd

# Start writing code
sf_public_salaries.groupby('basepay', as_index=False).mean()[['basepay','jobtitle']]

getting error message "['jobtitle'] not in index"

Does anyone know how to fix this?

https://platform.stratascratch.com/coding/9972-find-the-base-pay-for-police-captains?code_type=2

1
  • If this your actual code so far, I am not seeing where you have defined your data frame. If you have more code, please include it. Regards. Commented Jul 24, 2022 at 16:42

1 Answer 1

1

I don't know if I understand very well what you want, but if you are trying to get the media based on the 'basepay' and 'jobtitle' columns the following code solves the problem.

sf_public_salaries.groupby(['basepay','jobtitle'], as_index=False).mean()

The problem is occurring because the result of your code is a table with the column 'basepay' and the columns of numeric values that you can apply the mean function.

To select only the 'basepay' and 'jobtitle' columns just use the following code

 sf_public_salaries.groupby(['basepay','jobtitle'], as_index=False).mean()[['basepay', 'jobtitle']]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I want to show two columns 'basepay','jobtitle' only after group by base mean()..
Try this: sf_public_salaries.groupby(['basepay','jobtitle'], as_index=False).mean()[['basepay', 'jobtitle']]
Isn't the job title must be in the group by sections? if I try sf_public_salaries.groupby(['basepay','jobtitle'], as_index=False).mean()[['basepay', 'jobtitle','id']]...can get the id columns yet id column not appear in group by
What do you need with this code? You are using the average function, but you are discarding the numerical values that were calculated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.