美文网首页
python求平均

python求平均

作者: Aerosols | 来源:发表于2019-03-20 16:48 被阅读0次

    如果计算同一个东西,可以思考一下批量处理。

    计算日均、月均、年均,含缺测值,缺测天
    求污染等级有点。。。。

    import re
    import sys
    import numpy as np
    import pandas as pd
    
    xlsx = pd.read_excel('站点2018年1月1日-12月31日小时均值.xls','站点小时数据')
    xlsx.head()  #日期(年月日小时)和6项污染物
    
    #算PM25和PM10的比值,将大于1的设为空
    xlsx['bz']=xlsx['PM2.5'].div(xlsx['PM10'])   
    xlsx[xlsx['bz']>=1]
    xlsx.loc[xlsx.bz >= 1, ['PM2.5','PM10','bz'] ]= np.nan
    
    xlsx[xlsx['bz']>=1]  #复查
    
    #切片前先排序
    xlsx = xlsx.sort_index(level=0)
    
    #把年月日挑出来
    xlsx['year']=pd.to_datetime(xlsx['时间']).dt.year
    xlsx['month']=pd.to_datetime(xlsx['时间']).dt.month
    xlsx['day']=pd.to_datetime(xlsx['时间']).dt.day
    xlsx['hour']=pd.to_datetime(xlsx['时间']).dt.hour
    
    xlsx.drop(['城市名称','区县名称','站点名称','时间'],axis=1,inplace=True)
    xlsx.drop([0,], inplace=True)  #第一行不在时间范围内,去掉
    xlsx.head()
    
    xlsx = xlsx.set_index([xlsx['year'],xlsx['month'],xlsx['day'],xlsx['hour']])    #设置多重索引
    xlsx.drop(['year','month','day','hour'],axis=1,inplace=True)
    xlsx.head()
    
    # 算日均值
    day_data = xlsx.mean(level=[0,1,2])
    day_data.columns=["PM10","SO2","NO2","CO","O3","PM25","bz"]
    day_data.to_csv('day_data.csv', index=True, sep=',')
    
    #计算污染等级天数
    pm11=day_data[["PM10","PM25",'bz']][(day_data.PM10<=50)]
    pm11.count(level=0)
    
    pm12=day_data[["PM10","PM25",'bz']][(day_data.PM10>50)&(day_data.PM10<=150)]
    pm12.count(level=0)
    
    pm13=day_data[["PM10","PM25",'bz']][(day_data.PM10>150)&(day_data.PM10<=250)]
    pm13.count(level=0)
    
    pm14=day_data[["PM10","PM25",'bz']][(day_data.PM10>250)&(day_data.PM10<=350)]
    pm14.count(level=0)
    
    pm15=day_data[["PM10","PM25",'bz']][(day_data.PM10>350)&(day_data.PM10<=420)]
    pm15.count(level=0)
    
    pm16=day_data[["PM10","PM25",'bz']][(day_data.PM10>420)]
    pm16.count(level=0)
    
    
    pm21=day_data[["PM10","PM25",'bz']][(day_data.PM25<=35)]
    pm21.count(level=0)
    
    pm22=day_data[["PM10","PM25",'bz']][(day_data.PM25>35)&(day_data.PM25<=75)]
    pm22.count(level=0)
    
    pm23=day_data[["PM10","PM25",'bz']][(day_data.PM25>75)&(day_data.PM25<=115)]
    pm23.count(level=0)
    
    pm24=day_data[["PM10","PM25",'bz']][(day_data.PM25>115)&(day_data.PM25<=150)]
    pm24.count(level=0)
    
    pm25=day_data[["PM10","PM25",'bz']][(day_data.PM25>150)&(day_data.PM25<=250)]
    pm25.count(level=0)
    
    pm26=day_data[["PM10","PM25",'bz']][(day_data.PM25>250)]
    pm26.count(level=0)
    
    #算月均
    mon_data = xlsx.mean(level=[0,1])
    mon_data.to_csv('月均.csv', index=True, sep=',')
    mon_data
    
    #算年均
    year_data = xlsx.mean(level=0)
    year_data.to_csv('年均.csv', index=False, sep=',')
    year_data
    
    #算小时平均
    hour_data = xlsx.mean(level=[0,1,3])
    hour_data.to_csv('小时平均.csv', index=True, sep=',')
    hour_data
    
    hour_spr=hour_data.loc[(slice(2018,2018),slice(3,5), slice(None)), :].mean(level=2)
    hour_spr.to_csv('春季.csv', index=True, sep=',')
    hour_spr
    
    hour_sum=hour_data.loc[(slice(2018,2018),slice(6,8), slice(None)), :].mean(level=2)
    hour_sum.to_csv('夏季.csv', index=True, sep=',')
    hour_sum
    
    hour_aut=hour_data.loc[(slice(2018,2018),slice(9,11), slice(None)), :].mean(level=2)
    hour_aut.to_csv('秋季.csv', index=True, sep=',')
    hour_aut
    
    win1=hour_data.loc[(slice(2018,2018), slice(12,12), slice(None)), :].mean(level=2)
    win2 = hour_data.loc[(slice(2019,2019), slice(1,2), slice(None)), :].sum(level=2)
    win=(win1+win2)/3
    win.to_csv('冬季.csv', index=True, sep=',')
    
    

    计算臭氧滑动平均

    import re
    import sys
    import numpy as np
    import pandas as pd
    
    S1 = pd.read_excel('190308.S1','Sheet1') #时间、臭氧
    S1.head()
    
    S1['year']=pd.to_datetime(S1['时间']).dt.year
    S1['month']=pd.to_datetime(S1['时间']).dt.month
    S1['day']=pd.to_datetime(S1['时间']).dt.day
    S1['hour']=pd.to_datetime(S1['时间']).dt.hour
    
    S1 = S1.set_index([S1['year'],S1['month'],S1['day'],S1['hour']])
    S1.drop(['二氧化硫','PM10','时间','year','month','day','hour','二氧化氮','一氧化碳','PM25'],axis=1,inplace=True)
    S1.head()
    
    #初始滑动平均
    O3_pre1=S1.rolling(window=8).mean()
    
    #上移一位
    O3_pre2=O3_pre1.shift(-1)  #上调一位
    
    #算日均最大8小时平均
    O3_f=O3_pre2.loc[(slice(None), slice(None),slice(None),slice(7,23)), :].max(level=[0,1,2])
    O3_f.to_csv('臭氧日最大8小时滑动平均.csv', index=True, sep=',')
    
    O3_f[O3_f>160].count(level=1)
    
    #算月均、年均
    monave=O3_f.mean(level=[0,1])
    monave.to_csv('臭氧8小时滑动平均月均.csv', index=True, sep=',')
    monave
    
    yearave=O3_f.mean(level=[0])
    yearave.to_csv('臭氧8小时滑动平均年均.csv', index=True, sep=',')
    yearave
    
    Ospr=SO1.loc[(slice(2018,2018),slice(3,5), slice(None)), :].mean(level=0)
    Ospr.to_csv('春季.csv', index=True, sep=',')
    
    Osum=SO1.loc[(slice(2018,2018),slice(6,8), slice(None)), :].mean(level=0)
    Osum.to_csv('夏季.csv', index=True, sep=',')
    
    Oaut=SO1.loc[(slice(2018,2018),slice(9,11), slice(None)), :].mean(level=0)
    Oaut.to_csv('秋季.csv', index=True, sep=',')
    Oaut
    

    相关文章

      网友评论

          本文标题:python求平均

          本文链接:https://www.haomeiwen.com/subject/dfrtvqtx.html