Demographic Data Analyzer: TypeError: 'NoneType' object is not subscriptable

Hello everybody

I wrote the code to solve the Challenge for “Demographic Data Analyzer”.
I think that my code is correct, I checked the results and they seem fine to me. Anyway, when I try to test my code, it gives me 10 errors. The error is always the same: “TypeError: ‘NoneType’ object is not subscriptable”

I don’t understand where the issue is.

Here’s my code:

def calculate_demographic_data(print_data=True):

# Read data from file

  import pandas as pd

  df = pd.read_csv('adult.data.csv')

  # How many of each race are represented in this dataset? This should be a Pandas series with race names as the index labels.

  race_count = df['race'].value_counts()

  # What is the average age of men?

  average_age_men = round(df[df['sex']=='Male']['age'].mean(), 1)

  # What is the percentage of people who have a Bachelor's degree?

  mask = df['education']=='Bachelors'

  num_bachelors = df[mask].shape[0]

  percentage_bachelors = round(num_bachelors/(df.shape[0])*100, 1)

  # What percentage of people with advanced education (`Bachelors`, `Masters`, or `Doctorate`) make more than 50K?

  # What percentage of people without advanced education make more than 50K?

  # with and without `Bachelors`, `Masters`, or `Doctorate`

  higher_education = df[(df.education.isin(['Bachelors','Masters','Doctorate']))]

  lower_education = df[(~df.education.isin(['Bachelors','Masters','Doctorate']))]

  # percentage with salary >50K

  num_higher_education_rich = higher_education[higher_education['salary']=='>50K'].shape[0]

  higher_education_rich = round(num_higher_education_rich/(higher_education.shape[0])*100, 1)

  num_lower_education_rich = lower_education[lower_education['salary']=='>50K'].shape[0]

  lower_education_rich = round(num_lower_education_rich/(lower_education.shape[0])*100, 1)

  # What is the minimum number of hours a person works per week (hours-per-week feature)?

  min_work_hours = df['hours-per-week'].min()

  # What percentage of the people who work the minimum number of hours per week have a salary of >50K?

  num_min_workers = df[df['hours-per-week']==df['hours-per-week'].min()].shape[0]

  num_min_workers_over_50k = sum(df[df['hours-per-week']==df['hours-per-week'].min()]['salary']=='>50K')

  rich_percentage = round(num_min_workers_over_50k/num_min_workers*100, 1)

  # What country has the highest percentage of people that earn >50K?

  rich_perc_list = []

  for country in df['native-country'].unique():

    my_df = df[df['native-country']==country]

    # print(my_df.head())

    num_ppl_country_rich = my_df[my_df['salary']=='>50K'].shape[0]

    num_ppl_country = my_df.shape[0]

    # print(f"{country} has {num_ppl_country} persons")

    prc_ppl_rich = num_ppl_country_rich/num_ppl_country*100

    rich_perc_list.append(prc_ppl_rich)

    # print(f"Country = {country}, people with salary >50K = {prc_ppl_rich:3.2f}% ({num_ppl_country_rich} persons)")

  max_index = rich_perc_list.index(max(rich_perc_list))

  # print(f"Richest country: {df['native-country'].unique()[max_index]}")

  highest_earning_country = df['native-country'].unique()[max_index]

  highest_earning_country_percentage = round(max(rich_perc_list), 1)

  # Identify the most popular occupation for those who earn >50K in India.

  mask = (df['native-country']=='India')&(df['salary']=='>50K')

  my_series = df[mask]['occupation'].value_counts()

  top_IN_occupation = my_series[my_series==max(my_series)].index[0]

  # DO NOT MODIFY BELOW THIS LINE

  if print_data:

      print("Number of each race:\n", race_count) 

      print("Average age of men:", average_age_men)

      print(f"Percentage with Bachelors degrees: {percentage_bachelors}%")

      print(f"Percentage with higher education that earn >50K: {higher_education_rich}%")

      print(f"Percentage without higher education that earn >50K: {lower_education_rich}%")

      print(f"Min work time: {min_work_hours} hours/week")

      print(f"Percentage of rich among those who work fewest hours: {rich_percentage}%")

      print("Country with highest percentage of rich:", highest_earning_country)

      print(f"Highest percentage of rich people in country: {highest_earning_country_percentage}%")

      print("Top occupations in India:", top_IN_occupation)

      return {

          'race_count': race_count,

          'average_age_men': average_age_men,

          'percentage_bachelors': percentage_bachelors,

          'higher_education_rich': higher_education_rich,

          'lower_education_rich': lower_education_rich,

          'min_work_hours': min_work_hours,

          'rich_percentage': rich_percentage,

          'highest_earning_country': highest_earning_country,

          'highest_earning_country_percentage': highest_earning_country_percentage,

          'top_IN_occupation': top_IN_occupation

      }

Error suggests that function might not return anything, in such case python implicitly return None.

Check if indentation of return and if print_data: is the same in function. Function should always return that dictionary, not only if print_data is True.

1 Like

The error is self-explanatory. You are trying to subscript an object which you think is a list or dict, but actually is None. This means that you tried to do:

None[something]

This error means that you attempted to index an object that doesn’t have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. ‘NoneType’ object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn’t define the getitem method . This is a design principle for all mutable data structures in Python.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.