Beijing weather data csv processing

import csv

file = "data/BeijingPM20100101_20151231.csv"

with open(file) as f:
    # TODO
    # 完成如下两行代码,使用csv.reader将文件读入到二维列表data_list中
    reader =csv.reader(f)
    data_list = list(reader)

    # TODO
    # 打印data_list的前10行
    for i in range(10):
        print(data_list[i])

Data calculation

# TODO
# 将data_list中所有2015年的数据抽取出来,存储到列表data_2015中
data_2015=[]
for lines in data_list:
    if lines[1]=='2015':
        data_2015.append(lines)
        
# TODO
# 打印data_2015共有多少行组成
print(len(data_2015))

Data calculation

# TODO
# 将data_list中所有2015年的数据抽取出来,存储到列表data_2015中
data_2015=[]
for lines in data_list:
    if lines[1]=='2015':
        data_2015.append(lines)
        
# TODO
# 打印data_2015共有多少行组成
print(len(data_2015))

In the collected data for 2015, we hope to calculate the mean value of PM through the four numbers of PM_Dongsi, PM_Dongsihuan, PM_Nongzhanguan, and PM_US Post in the daily data.

If a certain piece of data is NA, this element does not need to be considered when calculating. If all four elements of the day are NA, the calculation result of the day is also NA.

Finally a new list is generated. The contents of each line of the list are:'No','year','month','day','hour','season','PM_Dongsi','PM_Dongsihuan','PM_Nongzhanguan', ' PM_US Post','PM_Avarage'.

data_new = [['No', 'year', 'month', 'day', 'hour', 'season', 'PM_Dongsi', 'PM_Dongsihuan', 'PM_Nongzhanguan', 'PM_US Post', 'PM_Avarage']]
for item in data_2015:
    # 获取每行前10列内容放入new_item
    new_item = item[:10]
    
    # TODO
    # 将pm相关的4列内容取出放到pm_slice中
    pm_slice = new_item[6:10]
    
    no_na_num = 0
    pm_sum = 0
    for i in range(4):
        if pm_slice[i] != 'NA':
            no_na_num += 1
            # TODO
            # 将pm_slice[i]累加到变量pm_sum中,注意pm_slice[i]是字符串类型,这里需要做类型转换
            pm_sum += int(pm_slice[i])
    if no_na_num == 0:
        # 如果4个元素都是0,结果列内容设置为'NA'
        new_item.append('NA')
    else:
        # TODO
        # 计算四个PM列的均值,存入变量pm_avarage变量中
        pm_avarage = pm_sum/4
        # TODO
        # 将新生成的pm_avarage值添加到new_item末尾
        new_item.append(pm_avarage)
        
    
    # 将new_item这行添加到data_new末尾
    data_new.append(new_item)
    
# TODO
# 打印data_new的前3行
for i in range(3):
    print(data_new[i])

Write back the result Write
the calculated result data_new back to the csv file.

with open('./data/BeijingPM_result.csv', 'w', newline='') as f:
    # TODO
    # 将data_new内容写出到文件./data/BeijingPM_result.csv
    writer =csv.writer(f)
    writer.writerows(data_new)
   

Guess you like

Origin blog.csdn.net/lildn/article/details/114489927