python 5回目のジョブ

10366 ワード

練習問題1:ファイルpmiを読み込むdays.csv、次の操作を完了します:1.統計品質等級の対応する日数、例えば:優:5日良:3日中度汚染:2日2.PMI 2を探し出す.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), "
:%d
" % len(good.index), "
:%d
" % len(mild.index),"
:%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]), "
PM2.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-2018 GDPを読み込む.csv、次の操作を完了します:1.行ごとに年間GDPデータを出力し、ヘッダー列名はファイル1行目に示す.
2.各年のGDPデータを辞書形式に変換し、年をkeys、その他の値をvalues(データ型をリスト形式)とする.例えば:{2017:[82712.7,212.7,6.8%,60989].......}
import pandas
path = open("1980-2018GDP.csv")
data = pandas.read_csv(path)
#       GDP  
print(data,end="

") # GDP , keys, values( ) dict = data.set_index(' ').T.to_dict('list') print("", end="") print(format(dict),end="

")
           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]}