I have a DataFrame (see image below) with daily values of which I want to retrieve a couple of values. I want to retrieve: Monthly mean, Monthly min, Monthly max, Yearly min, Amount of years etc. Currently I am resampling my DataFrame to retrieve these values (see code below) but there must be a better way.
T_monthly=pd.DataFrame() # Create dataframe for monthly temperatures
T_monthly['Basse']=temp_basse.iloc[:,3].resample("M").mean() # Add Basse
T_monthly['Basse_min']=temp_basse.iloc[:,3].resample("M").min()
T_monthly['Basse_max']=temp_basse.iloc[:,3].resample("M").max()
T_monthly['Kedougou']=temp_kedougou.iloc[:,3].resample("M").mean() # Add Kedougou
T_monthly['Kedougou_min']=temp_kedougou.iloc[:,3].resample("M").min()
T_monthly['Kedougou_max']=temp_kedougou.iloc[:,3].resample("M").max()
T_yearly=pd.DataFrame() # Create dataframe for yearly temperatures
T_yearly['Basse_min']=temp_basse.iloc[:,3].resample("Y").min()
T_yearly['Basse_max']=temp_basse.iloc[:,3].resample("Y").max()
T_yearly['Kedougou_min']=temp_kedougou.iloc[:,3].resample("Y").min()
T_yearly['Kedougou_max']=temp_kedougou.iloc[:,3].resample("Y").max()
Preferably I want to have a function where I can put in a date, eg 2014, 2 (Y, M) and retrieve all the necessary values without resampling to new DataFrames. For example:
input: (2014, 2)
output: [monthly mean, monthly min, monthly max, yearly min, yearly max]
At the moment my functions make use of the index number of the dataframes (see below). However, if I want to put in a certain date I first need to find the corresponding index number.
T_monthly.iloc[i, 3]
Thanks in advance!
Added two more columns after the sugestion of Sowjanya R Bhat, containing ['year'] and ['month']
df['year'] = pd.DatetimeIndex(df['date']).year
df['month'] = pd.DatetimeIndex(df['date']).month
Next selecting data by:
df.loc[(df['year'] == year) & (df['month'] == month)].iloc[:,3]
Not exactly how I wanted it (based on index), but great for looping through the data with the functions I wrote.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.