1.统计出生率

数据下载地址:https://github.com/fivethirtyeight/data/tree/master/births

U.S. births data
The raw data behind the story Some People Are Too Superstitious To Have A Baby On Friday The 13th
File:
US_births_1994-2003_CDC_NCHS.csv
contains U.S. births data for the years 1994 to 2003, as provided by the Centers for Disease Control and Prevention's National Center for Health Statistics

1.统计出生率_第1张图片

目的:统计每年/月/日出生人数,返回dictionary
过程很简单,主要目的在于体会Python语言的简单特性:

str_list=open('US_births_1994-2003_CDC_NCHS.csv').read().split('\n')

def calc_counts(data,col):
    births_per_col={}
    for line in data:
        if line[col] not in births_per_col:
            births_per_col[line[col]]=line[4]
        else:
            births_per_col[line[col]]+=line[4]
    return births_per_col

cdc_year_births=calc_counts(cdc_list,0)
print('births per year:',cdc_year_births) 
cdc_month_births=calc_counts(cdc_list,1)
print('births per month:',cdc_month_births) 
cdc_dom_births=calc_counts(cdc_list,2)
print('births per day of month:',cdc_dom_births) 
cdc_dow_births=calc_counts(cdc_list,3)
print('births per day of week:',cdc_dow_births) 

你可能感兴趣的:(1.统计出生率)