Home > Enterprise >  how to segregate monthly average data based on station wise using pandas?
how to segregate monthly average data based on station wise using pandas?

Time:12-17

I have 30 years of data that has been collected from 385 stations. I would like to calculate the monthly average of all years according to individual stations and export it into a CSV file. I am very new to coding I don't know how to execute this. please help someone to sort out my issues .herewith I have enclosed the code for one station. as same as like i should prepare csv file all 385 stations


#selective column only 
ap= data[data["station_id"]=='C0A520']
ap=ap[['station_id','TEMP','YEAR','MONTH']]
grouped = ap.groupby(by=["YEAR","MONTH"])

monthly_mean = grouped.mean()
monthly_mean.head()

#export groupby 
grouped.mean().reset_index().to_csv('D:/My_files/Research Progress/data/Temperature/final/coa520.csv')

CodePudding user response:

I am assuming that your existing code works as intended and that you do not want to write the code for each of the 385 stations. This can be achieved in a simple for loop iterating over the station names:

for station in data["station_id"].unique():

    # selective column only 
    ap= data[data["station_id"]==station]
    ap=ap[['station_id','TEMP','YEAR','MONTH']]
    grouped = ap.groupby(by=["YEAR","MONTH"])

    # export groupby 
    grouped.mean().reset_index().to_csv(f'D:/My_files/Research Progress/data/Temperature/final/{station}.csv')

(You did not use the monthly_mean variable, so I wrote it out)

  • Related