# Census Variables

hey, how to solve this part of the project?

Welcome to the forums!

What aspects of this step are you having trouble with? We’d much rather help you along to figure this out than to simply give you the solution.

2 Likes

This takes us to our version of that page, which means we still can’t see the code. Can you post it in the forums, and make sure you format it correctly

1 Like

I have the same problem with part, Can you please provide a guide for this project? thanks

Since this a learning environment it’s unlikely anyone would provide a step by step solution for you. If you browser around the forums and read the following- FAQ: How to ask good questions then you’ll see what kind of queries folks might be able and willing to help with. Break down your problem to its simplest point (minimal working example style) and say what it is you’ve tried to do to solve a problem and you’d likely receive a much better response.

1 Like

This is my code.

census[‘age’] = 2021 - census.birth_year

census[‘age_group’] = (census.age - (census.age % 5))/5

census[‘age_group’]=census[‘age_group’].astype(‘int’)

4 Likes

This furthers the post previous from jiayizhang444 , havnt figured out how to Label individual categories.
Category 2 would be equal to 10-15, Category 3 is equal to 15-20 and so on. Hopefully helps someone in the future

census[‘age’] = 2021 - census.birth_year

census[‘age_group’] = (census.age - (census.age % 5))/5

census[‘age_group’] = census[‘age_group’].astype(‘int’)

#print(census[‘age_group’].sort_values())

#print(census[‘age_group’].unique())

census[‘age_group’] = pd.Categorical(census[‘age_group’],[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], ordered=True)

print(census[‘age_group’])

1 Like

One more possible solution for you!

``````import numpy as np
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)
``````
4 Likes

Here is my solution, it’s probably not the cleanest one you can find, but it works xD

Screanshot of the code:

Screanshot of the result:

Hope this helps

1 Like

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