Python Forum
Create a monthly mean column in multiple years
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a monthly mean column in multiple years
#1
I have a 'dates' column and a 'tmax' (temperature values) column. I want to create one another column that shows each month's average in each year. I tried
    df['date']=pd.date_range(start='1/01/1980', end='31/12/2015')
    #df['date'] = df['date'].dt.strftime('%d-%b-%Y') format of date
    df['means'] = df.resample('M', on='date').mean()
I have got
ValueError: Wrong number of items passed 4, placement implies 1

Sample values are below:
date tmax
1-Jan-80 15.773
2-Jan-80 18.342
...
30-Jan-80 15.851
31-Jan-80 11.962
...
1-Dec-80 15.773
2-Dec-80 18.342
...
30-Dec-80 15.851
31-Dec-80 11.962
1-Jan-81 15.773
2-Jan-81 18.342
...
30-Jan-81 15.851
31-Jan-81 11.962
...
1-Dec-2015 15.773
2-Dec-2015 18.342
...
30-Dec-2015 15.851
31-Dec-2015 11.962
Reply
#2
grouper = pd.Grouper(key='date', freq='M')
df.groupby(grouper).mean() # df.groupby(grouper).tmax.mean()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  havent programmed in years - confused by why RETURN is not returning stmoose 5 1,185 Mar-26-2023, 05:51 AM
Last Post: buran
  Resample from monthly to weekly works, but not vice versa JaneTan 0 587 Dec-14-2022, 12:58 AM
Last Post: JaneTan
  create new column based on condition arvin 12 2,253 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  Create multiple/single csv file for each sql records mg24 6 1,405 Sep-29-2022, 08:06 AM
Last Post: buran
  How to combine multiple column values into 1? cubangt 15 2,869 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Python create a spreadsheet with column and row header ouruslife 4 1,646 Jul-09-2022, 11:01 AM
Last Post: Pedroski55
  Reshaping a single column in to multiple column using Python sahar 7 2,074 Jun-20-2022, 12:35 PM
Last Post: deanhystad
  df column aggregate and group by multiple columns SriRajesh 0 1,050 May-06-2022, 02:26 PM
Last Post: SriRajesh
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,541 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Split single column to multiple columns SriRajesh 1 1,338 Jan-07-2022, 06:43 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020