文章目录
美国1880-2010年的婴儿名字
美国社会保障局(SSA)提供了从1880年至现在的婴儿姓名频率的数据。可以使用这些数据做很多事情:
根据给定的名字对婴儿名字随时间的比例进行可视化
确定一个名字的相对排位
确定每年最受欢迎的名字,或者流行程度最高或最低的名字
我们对美国1880-2010年的婴儿名字进行了一些分析。以下是分析的主要结果:
每年男性和女性婴儿的总出生人数:
total_births = names.pivot_table("births", index="year", columns="sex", aggfunc=sum)
total_births.plot(title="Total births by sex and year")
每个名字在每年中的占比:
names = names.groupby(["year", "sex"], group_keys=False).apply(add_prop)
获取每年最受欢迎的1000个名字:
def get_top1000(group):
return group.sort_values("births", ascending=False)[:1000]
grouped = names.groupby(["year", "sex"])
top1000 = grouped.apply(get_top1000)
统计特定名字(如John、Harry、Mary、Marilyn)随时间的变化:
subset = total_births[["John", "Harry", "Mary", "Marilyn"]]
subset.plot(subplots=True, figsize=(12, 10), title="Number of births per year")
分析名字多样性,即每年受欢迎的名字的数量:
diversity = top1000.groupby(["year", "sex"]).apply(get_quantile_count)
diversity = diversity.unstack()
diversity.plot(title="Number of popular names in top 50%")
分析每个年代结尾字母在男性名字中的变化趋势:
last_letters = names["name"].map(get_last_letter)
table = names.pivot_table("births", index=last_letters, columns=["sex", "year"], aggfunc=sum)
subtable = table.reindex(columns=[1910, 1960, 2010], level="year")
subtable.plot(kind="bar", figsize=(10, 8))
分析特定名字(如Leslie)在不同性别中的变化趋势:
lesley_like = all_names[all_names.str.contains("Lesl")]
filtered = top1000[top1000["name"].isin(lesley_like)]
table = filtered.pivot_table("births", index="year", columns="sex", aggfunc="sum")
table = table.div(table.sum(axis="columns"), axis="index")
table.plot(style={"M": "k-", "F": "k--"})
以上是对美国1880-2010年婴儿名字数据集的一些分析。