Course1.winona.edu



Reading in Multiple *.csv filesBaby Name Website: Download the NationalData file. Unzip this file into its own directory. A snippet of the file contained are shown here.The actual *.txt files have the following structure.Code to read in multiple *.csv filesimport pandas as pdfrom pandas import Series, DataFramenames1880 = pd.read_csv('C:/DSCI210/Datasets/Babynames/names/yob1880.txt', names = ['name','sex','births'])names1880.groupby('sex')['births'].sum()years = range(1880,2014)pieces = [ ] columns = ['name','sex','births']for year in years: path = 'C:/Teaching/DSCI210/Datasets/Babynames/names/yob%d.txt' % year frame = pd.read_csv(path, names = columns) frame['year'] = year pieces.append(frame)#Putting pieces into a pandas DataFramenames = pd.concat(pieces,ignore_index = True)names#Getting total births by year and sextotal_births = names.pivot_table('births',rows='year',cols='sex',aggfunc=sum)#Making a simple time series plottotal_births.plot()#Get first 10 rows of namesnames[:10]#Working with names = Chrisonlychris = names[names.name == 'Chris']#Get a breakdown of male Chris vs. female Chrisonlychristable = onlychris.pivot_table('births',rows='year',cols='sex',aggfunc=sum)onlychristableonlychristable.index#Putting onlychris data into a DataFrameonlychristable2 = DataFrame(index=onlychristable.index)#Computing the ratio between Male / Female; if Ratio = 1, then #male chris = # female chrisonlychristable2['malevsfemale'] = onlychristable.M / onlychristable.F#Another plot of ratioonlychristable2.plot() ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download