写在前面的话:
实例中的所有数据都是在GitHub上下载的,打包下载即可。
地址是:/pydata/pydata-book
还有一定要说明的:
我使用的是Python2.7,书中的代码有一些有错误,我使用自己的2.7版本调通。
# coding: utf-8import pandas as pdimport numpy as npnames1880 = pd.read_csv('D:\Source Code\pydata-book-master\ch02\\names\yob1880.txt', names=['name','sex','births'])names1880names1880.groupby('sex').births.sum()years = range(1880, )pieces = []columns = ['name','sex','births']for year in years:path = 'D:\Source Code\pydata-book-master\ch02\\names\yob%d.txt' %yearframe = pd.read_csv(path, names=columns)frame['year'] = yearpieces.append(frame)names = pd.concat(pieces,ignore_index=True)namestotal_births = names.pivot_table('births', index='year', columns='sex', aggfunc=sum)total_births.tail()total_births.plot(title='Total births by sex and year')def add_group(group):births = group.births.astype(float)group['prop'] = births / births.sum()return groupnames = names.groupby(['year','sex']).apply(add_group)namesnp.allclose(names.groupby(['year','sex']).prop.sum(),1)def get_top1000(group):return group.sort_index(by='births',ascending=False)[:1000]grouped = names.groupby(['year','sex'])top1000 = grouped.apply(get_top1000)top1000boys = top1000[top1000.sex == 'M']girls = top1000[top1000.sex == 'F']total_births = top1000.pivot_table('births', index='year', columns='name', aggfunc=sum)total_birthssubset = total_births[['John','Harry','Mary','Marilyn']]subset.plot(subplots=True,figsize=(12,10),grid=False,title="Number of births per year")table = top1000.pivot_table('prop',index='year',columns='sex',aggfunc=sum)table.plot(title='sum of table1000.prop by year and sex',yticks=np.linspace(0,1.2,13),xticks=range(1880,,10))df = boys[boys.year == ]dfprop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum()prop_cumsum[:10]prop_cumsum.searchsorted(0.5)df = boys[boys.year == 1900]in1900 = df.sort_index(by='prop', ascending=False).prop.cumsum()in1900.searchsorted(0.5)+1def get_quantile_count(group, q=0.5):group = group.sort_index(by='prop',ascending=False)return group.prop.cumsum().searchsorted(q)+1diversity = top1000.groupby(['year','sex']).apply(get_quantile_count)diversity = diversity.unstack('sex')diversity.head()get_last_letter = lambda x:x[-1]last_letter = names.name.map(get_last_letter)last_letter.name = 'last_letter'table = names.pivot_table('births', index=last_letter, columns=['sex','year'], aggfunc=sum)subtable = table.reindex(columns=[1910,1960,], level='year')subtable.head()subtable.sum()letter_prop = subtable / subtable.sum().astype(float)import matplotlib.pyplot as pltfig,axex = plt.subplots(2,1,figsize=(10,8))letter_prop['M'].plot(kind='bar',rot=0,ax=axex[0],title='Male')letter_prop['F'].plot(kind='bar',rot=0,ax=axex[1],title='Female',legend=False)letter_prop = table / table.sum().astype(float)dny_ts = letter_prop.ix[['d','n','y'],'M'].Tdny_ts.head()dny_ts.plot()all_names = top1000.name.unique()mask = np.array(['lesl' in x.lower() for x in all_names])lesley_like = all_names[mask]lesley_likefiltered = top1000[top1000.name.isin(lesley_like)]filtered.groupby('name').births.sum()table = filtered.pivot_table('births', index='year', columns='sex', aggfunc = 'sum')table = table.div(table.sum(1), axis = 0)table.tail()table.plot(style={'M':'k-','F':'k--'})