习题1:读入文件pmi_days.csv,完成以下操作:
1.统计质量等级对应的天数,例如:
优:5天
良:3天
中度污染:2天
2.找出PMI2.5的最大值和最小值,分别指出是哪一天。
import pandas import numpy file = open(r " pmi_days.csv " ) df = pandas.read_csv(file) date = df.groupby( ' 质量等级 ' ) excellent = dict([x for x in date])[ ' 优 ' ] good = dict([x for x in date])[ ' 良 ' ] mild = dict([x for x in date])[ ' 轻度污染 ' ] mid = dict([x for x in date])[ ' 中度污染 ' ] print ( " 优:%d天 " % len(excellent.index), " \n良:%d天 " % len(good.index), " \n轻度污染:%d天 " % len(mild.index), " \n中度污染:%d天 " % len(mid.index)) sortpm =df.sort_values(by= ' PM2.5 ' ) sortpm2 =sortpm.reset_index(drop= True) print ( " PM2.5最大的一天是:%s。数值:%d " % (sortpm2[ ' 日期 ' ][29], sortpm2[ ' PM2.5 ' ][29 ]), " \nPM2.5最小的一天是:%s。数值:%d " % (sortpm2[ ' 日期 ' ][0], sortpm2[ ' PM2.5 ' ][0]))
优:4天 良:16天 轻度污染:9天 中度污染:1天 PM2.5最大的一天是:2019/4/29。数值:106 PM2.5最小的一天是:2019/4/25。数值:9
习题2:读入文件1980-2018GDP.csv,完成以下操作:
1.按行输出每年GDP数据,表头列名如文件第1行所示。
2.将各年GDP数据转换成字典格式,以年份为keys,其它值为values(数据类型为列表方式),例如:
{
2017:[827121.7,6.8%,60989]
........
}
import pandas path = open( " 1980-2018GDP.csv " ) data = pandas.read_csv(path) # 按行输出每年GDP数据 print (data,end= " \n\n " ) # 将各年GDP数据转换成字典格式,以年份为keys,其它值为values(数据类型为列表方式) dict = data.set_index( ' 年份 ' ).T.to_dict( ' list ' ) print ( " 字典: " , end= "" ) print (format(dict),end= " \n\n " )
年份 GDP(亿元) GDP增长率 人均GDP(元/人) 通货膨胀率 0 1978 3645.2 0.00% 381 NaN 1 1979 4062.6 7.60% 419 NaN 2 1980 4545.6 7.80% 463 6.00% 3 1981 4891.6 5.30% 492 2.40% 4 1982 5323.4 9.00% 528 1.90% 5 1983 5962.7 10.90% 583 1.50% 6 1984 7208.1 15.20% 695 2.80% 7 1985 9016.0 13.50% 858 9.40% 8 1986 10275.2 8.90% 963 6.50% 9 1987 12058.6 11.60% 1112 7.30% 10 1988 15042.8 11.30% 1366 18.80% 11 1989 16992.3 4.10% 1519 18.00% 12 1990 18667.8 3.80% 1644 3.10% 13 1991 21781.5 9.20% 1893 3.40% 14 1992 26923.5 14.20% 2311 6.40% 15 1993 35333.9 14.00% 2998 14.70% 16 1994 48197.9 13.10% 4044 24.10% 17 1995 60793.7 10.90% 5046 17.10% 18 1996 71176.6 10.00% 5846 8.30% 19 1997 78973.0 9.30% 6420 2.80% 20 1998 84402.3 7.80% 6796 -0.80% 21 1999 89677.1 7.60% 7159 -1.40% 22 2000 99214.6 8.40% 7858 0.40% 23 2001 109655.2 8.30% 8622 0.70% 24 2002 120332.7 9.10% 9398 -0.80% 25 2003 135822.8 10.00% 10542 1.20% 26 2004 159878.3 10.10% 12336 3.90% 27 2005 183084.8 10.20% 14040 1.80% 28 2006 211923.8 11.60% 16024 1.50% 29 2007 249530.6 11.90% 18868 4.80% 30 2008 300670.0 9.00% 23128 5.90% 31 2009 340507.0 9.10% 25608 -0.70% 32 2010 397980.0 10.30% 30015 3.30% 33 2011 471564.0 9.20% 36403 5.40% 34 2012 519322.0 7.80% 40007 2.60% 35 2013 568845.0 7.70% 43852 2.60% 36 2014 636463.0 7.40% 47203 2% 37 2015 677000.0 6.90% 50251 3% 38 2016 744127.0 6.70% 53935 2% 39 2017 827121.7 6.80% 60989 NaN 40 2018 900309.0 6.6 65484 NaN
字典:{1978: [3645.2, '0.00%', 381, nan], 1979: [4062.6, '7.60%', 419, nan], 1980: [4545.6, '7.80%', 463, '6.00%'], 1981: [4891.6, '5.30%', 492, '2.40%'], 1982: [5323.4, '9.00%', 528, '1.90%'], 1983: [5962.7, '10.90%', 583, '1.50%'], 1984: [7208.1, '15.20%', 695, '2.80%'], 1985: [9016.0, '13.50%', 858, '9.40%'], 1986: [10275.2, '8.90%', 963, '6.50%'], 1987: [12058.6, '11.60%', 1112, '7.30%'], 1988: [15042.8, '11.30%', 1366, '18.80%'], 1989: [16992.3, '4.10%', 1519, '18.00%'], 1990: [18667.8, '3.80%', 1644, '3.10%'], 1991: [21781.5, '9.20%', 1893, '3.40%'], 1992: [26923.5, '14.20%', 2311, '6.40%'], 1993: [35333.9, '14.00%', 2998, '14.70%'], 1994: [48197.9, '13.10%', 4044, '24.10%'], 1995: [60793.7, '10.90%', 5046, '17.10%'], 1996: [71176.6, '10.00%', 5846, '8.30%'], 1997: [78973.0, '9.30%', 6420, '2.80%'], 1998: [84402.3, '7.80%', 6796, '-0.80%'], 1999: [89677.1, '7.60%', 7159, '-1.40%'], 2000: [99214.6, '8.40%', 7858, '0.40%'], 2001: [109655.2, '8.30%', 8622, '0.70%'], 2002: [120332.7, '9.10%', 9398, '-0.80%'], 2003: [135822.8, '10.00%', 10542, '1.20%'], 2004: [159878.3, '10.10%', 12336, '3.90%'], 2005: [183084.8, '10.20%', 14040, '1.80%'], 2006: [211923.8, '11.60%', 16024, '1.50%'], 2007: [249530.6, '11.90%', 18868, '4.80%'], 2008: [300670.0, '9.00%', 23128, '5.90%'], 2009: [340507.0, '9.10%', 25608, '-0.70%'], 2010: [397980.0, '10.30%', 30015, '3.30%'], 2011: [471564.0, '9.20%', 36403, '5.40%'], 2012: [519322.0, '7.80%', 40007, '2.60%'], 2013: [568845.0, '7.70%', 43852, '2.60%'], 2014: [636463.0, '7.40%', 47203, '2%'], 2015: [677000.0, '6.90%', 50251, '3%'], 2016: [744127.0, '6.70%', 53935, '2%'], 2017: [827121.7, '6.80%', 60989, nan], 2018: [900309.0, '6.6', 65484, nan]}