Finalised program -Chapter 17 (Data Cleaning with Pandas) -US Census Data

would someone have the final code for the ‘US Census Data’ project in Module 17 in Data Science. I have pasted what I could do so far…gets wrong with lines with ‘duplicate()’…


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import codecademylib3_seaborn
import glob

files =glob.glob(“states*.csv”)
df_list =

for filename in files:
data =pd.read_csv(filename)
df_list.append(data)

df =pd.concat(df_list)
#print(df.head(5))

df[‘Income’] =df[‘Income’].replace(’[$,]’,’’, regex =True)
pd.Income =pd.to_numeric(df.Income)

df[‘str_split’]=df.GenderPop.str.split(’_’)
df[‘men’]=df.str_split.str.get(0)
df[‘women’]=df.str_split.str.get(1)

df.men=df.men.replace(’[“M”,]’,’’,regex =True)
df.women=df.women.replace(’[“F”,]’,’’,regex =True)

df.men =pd.to_numeric(df.men)
df.women =pd.to_numeric(df.women)

plt.scatter(df.men,df.women)
plt.show()

df=df.dropna(subset=[‘women’])
df=df.fillna(value =df.TotalPop-df.men)

df =df.duplicated()
df =df.drop_duplicates()


thanks…

hey! got the code here

2 Likes

many thanks…sergialess… : )

what editor did you use to write the program.

hi thanks Sergialess, the solution code u found is good but doesnt work in codecademy…
Codecademy hasnt even given appropriates download files… this just keeps happening
. Amazing you think we are actually trying to learn something and they just throw crap at us without explaining or any video turorials. Seems the deeper you go into Data Science the lazier this site is getting. It’s quite shocking that anyone isnt even replying to your posts or others aswell , think ill give up after 6 hours lol.
Thanks again

Jupyter notebook was used. You can use terminal, command line to install the basic version that is going to let you work inside you browser… the other solution is to install Anaconda (https://www.anaconda.com/) and you can load various tools.

hope this helps

Many thanks this complete version. This exercise missed the valuable tutorial that comes with… and as an analyst, data cleaning is one of the major function with need to master <3