Project : Census Variables

Hello !
Could someone share with me the code on how to solve this part of the project?

> * Create a new variable called marital_codes by Label Encoding the marital_status variable. This could help the Census team use machine learning to predict if a respondent thinks the wealthy should pay higher taxes based on their marital status.

As this is a learning environment simply asking for code if frowned upon, the community guidelines are worth viewing-

You’d likely get a better response by following the guidance laid out in the following FAQ on how best to set up questions (by and large these forums typically follow more of a Q&A style especially under “get-help”-


Dear fellow coders,

Please find here the solution for the census variables project

import codecademylib3

# Import pandas with alias
import pandas as pd

# Read in the census dataframe
census = pd.read_csv('census_data.csv', index_col=0)


# 3

# 4

# 5. 
census['birth_year'] = census['birth_year'].replace('missing', 1967)

census['birth_year'] = census['birth_year'].astype('int')

# 9

# converting type of columns to 'category'
census['higher_tax'] = census['higher_tax'].astype('category')

#  encoding
census['higher_tax'] = census['higher_tax']

# print out the median of the higher_tax variable

# 10
census = pd.get_dummies(census, columns = ['marital_status'] )



ths is my code, Appreciate your feedback, thank


Thanks for your sharing. :grinning_face_with_smiling_eyes:

1 Like

Hi guys, here’s my solution.

import codecademylib3
import numpy as np
# Import pandas with alias
import pandas as pd

# Read in the census dataframe
census = pd.read_csv('census_data.csv', index_col=0)


#check why birth year was classed as an object

#change the missing data to 1967
census['birth_year'] = census['birth_year'].replace(['missing'], 1967)
#change birth year data type to int
census['birth_year'] = census['birth_year'].astype('int')

#average birth year

#convert higher_tax to categorical
census['higher_tax'] = pd.Categorical(census['higher_tax'], ['strongly disagree', 'disagree', 'neutral', 'agree', 'strongly agree'], ordered=True)

census['higher_tax'] = census['higher_tax']

# median for higher tax is neutral

#census = pd.get_dummies(census, columns=['marital_status'])

census['marital_status'] = pd.Categorical(census['marital_status'], ['single', 'married', 'divorced', 'widowed'], ordered=True)
census['marital_codes'] = census['marital_status']

#Create an age column and group them by 5 year intervals
census['age'] = 2021 - census['birth_year']
age_bins = np.arange(min(census['age'])- 4, 100, 5)
census['age_group'] = pd.cut(census['age'], bins=age_bins)

#Python recognises the age_group variable as categorical. Encode age groups.
census['age_group'] = census['age_group']


Hello there,
I have 2 doubts in this learning project-

  1. I couldn’t understand what this code does-
census['higher_tax']= census['higher_tax']
#output is 2.0

Can you please explain what is, I checked the documentation but couldn’t grasp the concept.
2. I couldn’t understand the median output as well.

Please help. Will appreciate. :slight_smile:

You’re creating a column called higher_tax and then converting the categorical variables to numbers. returns a series of numbers (and the index) that correspond with the categorical variables in that column, higher_tax.

So, this:

'strongly disagree', 'disagree', 'neutral', 'agree', 'strongly agree'

df: index & response:
0  strongly disagree
1  disagree
2  neutral
3  agree
4  strongly agree

Becomes something like:

[1, 2, 3, 4, 5]

index, column in df:
0 1
1 2
2 3
3 4
4 5

Which you can then perform descriptive stats on. In this case using median to obtain the median value (response) of that column. So, you found that 2.0 is the median response to that question in the census.

“Codes are an array of integers which are the positions of the actual values in the categories array.” (from the docs below)


1 Like

Ahh so, the median output is ‘Neutral’. Got it now.
Thank you @lisalisaj! This was helpful! : )

1 Like

you’re welcome. :slight_smile:
Median is the middle value in the data sample that separates 50% of the lower values and 50% of the higher values. It’s the central tendency. (And, sometimes it’s more accurate than the mean, especially if there are outliers involved in the data.)

1 Like