实验要求一
data_Mete[ 'TIMESTAMP' ] = pd.to_datetime( data_Mete[ 'TIMESTAMP' ] ,format= '%Y%m%d%H%M%S' )
data_VI[ 'Date' ] = pd.to_datetime( data_VI[ 'Date' ] ,format= '%Y/%m/%d' )
print( data_VI[ 'Date' ] )
data_Mete[ 'Date' ] = [ i.date( ) for i in data_Mete[ 'TIMESTAMP' ] ]
data_Mete_group = data_Mete.groupby( by= 'Date' ,as_index= False)
data1 = data_Mete_group.agg( { 'TA' :[ np.max,np.min,np.mean] ,'P' :np.sum,'RG' :np.mean,'GPP' :np.mean,'GPP_QC' :calpercent} )
print( data1)
import numpy as np
import pandas as pd
from matplotlib import pyplot as pltdef calpercent( data) :cnt01 = sum( data== 0 ) +sum( data== 1 ) return cnt01/data.size
data_Mete = pd.read_csv( './data/Mete_FR-Gri.csv' )
data_VI = pd.read_csv( './data/VI_FR-Gri.csv' )
data_Mete[ 'TIMESTAMP' ] = pd.to_datetime( data_Mete[ 'TIMESTAMP' ] ,format= '%Y%m%d%H%M%S' )
print( data_Mete[ 'TIMESTAMP' ] )
data_VI[ 'Date' ] = pd.to_datetime( data_VI[ 'Date' ] ,format= '%Y/%m/%d' )
print( data_VI[ 'Date' ] )
data_Mete[ 'Date' ] = [ i.date( ) for i in data_Mete[ 'TIMESTAMP' ] ]
data_Mete_group = data_Mete.groupby( by= 'Date' ,as_index= False)
data1 = data_Mete_group.agg( { 'TA' :[ np.max,np.min,np.mean] ,'P' :np.sum,'RG' :np.mean,'GPP' :np.mean,'GPP_QC' :calpercent} )
print( data1)
data1.columns= [ 'Date' ,'TA_Max' , 'TA_Min' , 'TA_Mean' , 'P' , 'RG' , 'GPP' , 'GPP_QC' ] data1[ 'Date' ] = pd.to_datetime( data1[ 'Date' ] )
print( data1[ 'Date' ] )
mergedata = pd.merge( data1,data_VI,on= 'Date' ) mergedata.to_csv( './1test5_1.csv' )
print( '合并表格完成' )
实验二要求
全部代码
import numpy as np
import pandas as pd
from matplotlib import pyplot as pltdef calpercent( data) :cnt01 = sum( data== 0 ) +sum( data== 1 ) return cnt01/data.size
data_Mete = pd.read_csv( './data/Mete_FR-Gri.csv' )
data_VI = pd.read_csv( './data/VI_FR-Gri.csv' )
data_Mete[ 'TIMESTAMP' ] = pd.to_datetime( data_Mete[ 'TIMESTAMP' ] ,format= '%Y%m%d%H%M%S' )
print( data_Mete[ 'TIMESTAMP' ] )
data_VI[ 'Date' ] = pd.to_datetime( data_VI[ 'Date' ] ,format= '%Y/%m/%d' )
print( data_VI[ 'Date' ] )
data_Mete[ 'Date' ] = [ i.date( ) for i in data_Mete[ 'TIMESTAMP' ] ]
data_Mete_group = data_Mete.groupby( by= 'Date' ,as_index= False)
data1 = data_Mete_group.agg( { 'TA' :[ np.max,np.min,np.mean] ,'P' :np.sum,'RG' :np.mean,'GPP' :np.mean,'GPP_QC' :calpercent} )
print( data1)
data1.columns= [ 'Date' ,'TA_Max' , 'TA_Min' , 'TA_Mean' , 'P' , 'RG' , 'GPP' , 'GPP_QC' ] data1[ 'Date' ] = pd.to_datetime( data1[ 'Date' ] )
print( data1[ 'Date' ] )
mergedata = pd.merge( data1,data_VI,on= 'Date' ) mergedata.to_csv( './1test5_1.csv' )
print( '合并表格完成' ) def fixGppQc( data) :if sum( data> = 0.5 ) < 15 :return 2 else:return 1
data0 = mergedata
data0[ 'newdate' ] = data0[ 'Date' ] .map( lambda x: 10000 *x.year + 100 *x.month+1)
data0[ 'TA_Range' ] = data0[ 'TA_Max' ] -data0[ 'TA_Min' ]
data0group = data0.groupby( by= 'newdate' ,as_index= False)
data2 = data0group.agg( { 'TA_Range' :np.mean,'TA_Mean' :np.mean,'P' :np.sum,'RG' :np.mean,'GPP' :np.mean,'GPP_QC' :fixGppQc,'EVI' :np.mean,'NDVI' :np.mean} )
data2.loc[ data2[ 'GPP_QC' ] == 2 ,'GPP' ] = np.NaN
data2 = data2.rename( columns= { 'newdate' : 'Date' } )
data2[ 'Date' ] = pd.to_datetime( data2[ 'Date' ] ,format= '%Y%m%d' )
data3 = data2.dropna( )
plt.rcParams[ 'font.sans-serif' ] = [ 'SimHei' ]
plt.rcParams[ 'axes.unicode_minus' ] = False plt.figure( figsize= ( 8,7 .2)) print( '画出各个因素与光合作用的相关性柱状图' )
cols = [ 'TA_Mean' ,'TA_Range' ,'RG' ,'P' ,'NDVI' ]
corrs = [ data3[ 'GPP' ] .corr( data3[ i] ) for i in cols]
index = np.arange( len( cols)) for col,corr in zip( cols,corrs) :plt.bar( col,corr) plt.ylabel( '与GPP的相关系数' ,fontsize= 12 )
plt.xlabel( '与GPP的相关因素' ,fontsize= 12 )
plt.legend( [ '平均气温' ,'气温日较差' ,'太阳辐射' ,'降水' ,'NDVI' ] ,loc= 2 )
plt.savefig( './test5_2.jpg' )
plt.show( ) input( '按回车结束' )