推荐github上的这个课程来练习下,做个入门
按照名字将数据分组,总数,平均数,标准差
print(baby_names.groupby('name').agg([np.sum,np.mean,np.std]))
哪些名字出现的频率最高?
# 哪些名字出现的频率最高?print(baby_names.groupby('name').agg({'frequency': sum}))
# James, John, Robert, Micheal, Mary...都是耳熟能详的名字baby_names.groupby('name').agg({'frequency': sum}).sort_values(by=['frequency'], ascending=[0])
每年出生的男孩和女孩的个数分别是多少?
# 使用pivot_table方法查看freq_by_gender_year = baby_names.pivot_table(index ='year', columns='gender', values = 'frequency', aggfunc=sum)
# 使用tail方法查看最近几年出生人数print(freq_by_gender_year.tail())
# 一行命令即可做出高质量图形freq_by_gender_year.plot(title='Frequency by year and gender')plt.show()
起名趋势分析
#增加一个变量rank,这个是根据年份性别依据名字出现频率所产生的次序baby_names['ranked'] = baby_names.groupby(['year','gender'])['frequency'].rank(ascending=False)print(baby_names.head(10))
#计算每个名每年按性别占总出生人数的百分比def add_pct(group):#自定义 group['pct'] = group.frequency / group.frequency.sum()*100 return group#groupby和apply函数baby_names = baby_names.groupby(['year','gender']).apply(add_pct)# 查看新加的百分比(pct)print(baby_names.head())
查看每年最流行的名字所占百分比趋势
####起名趋势分析####增加一个变量rank,这个是根据年份性别依据名字出现频率所产生的次序baby_names['ranked'] = baby_names.groupby(['year','gender'])['frequency'].rank(ascending=False)# print(baby_names.head(10))#计算每个名每年按性别占总出生人数的百分比def add_pct(group):#自定义 group['pct'] = group.frequency / group.frequency.sum()*100 return group# #groupby和apply函数baby_names = baby_names.groupby(['year','gender']).apply(add_pct)# # 查看新加的百分比(pct)# print(baby_names.head())#####查看每年最流行的名字所占百分比趋势#####将数据分为男孩和女孩dff = baby_names[baby_names.gender == 'F']dfm = baby_names[baby_names.gender == 'M']#获取每年排名第一的名字rank1m = dfm[dfm.ranked == 1]rank1f = dff[dff.ranked == 1]plt.plot(rank1m.year, rank1m.pct, color="blue", linewidth = 2, label = 'Boys')plt.fill_between(rank1m.year, rank1m.pct, color="blue", alpha = 0.1, interpolate=True)plt.xlim(1880,2012)plt.ylim(0,9)plt.xticks(scipy.arange(1880,2012,10), rotation=70)plt.title("Popularity of #1 boys' name by year", size=18, color="blue")plt.xlabel('Year', size=15)plt.ylabel('% of male births', size=15)plt.show()plt.close()
plt.plot(rank1f.year, rank1f.pct, color="red", linewidth = 2, label = 'Girls')plt.fill_between(rank1f.year, rank1f.pct, color="red", alpha = 0.1, interpolate=True)plt.xlim(1880,2012)plt.ylim(0,9)plt.xticks(scipy.arange(1880,2012,10), rotation=70)plt.title("Popularity of #1 girls' name by year", size=18, color="red")plt.xlabel('Year', size=15)plt.ylabel('% of female births', size=15)plt.show()plt.close()