第五次作业
习题1:读入文件pmi_days.csv,完成以下操作:
1.统计质量等级对应的天数,例如:
优:5天
良:3天
中度污染:2天
2.找出PMI2.5的最大值和最小值,分别指出是哪一天。
import pandas as pd import numpy as np days_path = open(r"D:\Python\sst\venv\pmi_days.csv") days_df = pd.read_csv(days_path) data = days_df.groupby('质量等级') day_excellent = dict([x for x in data])['优'] day_good = dict([x for x in data])['良'] day_mild = dict([x for x in data])['轻度污染'] day_moderate = dict([x for x in data])['中度污染'] print("优:%d天" % len(day_excellent.index), "\n良:%d天" % len(day_good.index), "\n轻度污染:%d天" % len(day_mild.index), "\n中度污染:%d天" % len(day_moderate.index)) sort_pm25 = days_df.sort_values(by='PM2.5') sort_pm25_2 = sort_pm25.reset_index(drop=True) print("PM2.5最大值:%d 日期为:%s" % (sort_pm25_2['PM2.5'][29],sort_pm25_2['日期'][29]), "\nPM2.5最小值:%d 日期为:%s" % (sort_pm25_2['PM2.5'][0],sort_pm25_2['日期'][0]))
习题2:读入文件1980-2018GDP.csv,完成以下操作:
1.按行输出每年GDP数据,表头列名如文件第1行所示。
2.将各年GDP数据转换成字典格式,以年份为keys,其它值为values(数据类型为列表方式)
import pandas as pd days_path = open(r"D:\Python\sst\venv\1980-2018GDP.csv") days_list = pd.read_csv(days_path) print(days_list, "\t\t\n") dict_GDP = days_list.set_index('年份').T.to_dict('list') print("字典:", dict_GDP,"\n") data_max = max(dict_GDP, key=dict_GDP.get) data_min = min(dict_GDP, key=dict_GDP.get) print("GDP最大值是:", data_max, dict_GDP[data_max], "\n") print("GDP最小值是:", data_min, dict_GDP[data_min])