Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
groupby question
#1
I hope you are all having a good day. I have a DataFrame named df with 9 variables and 99,492 observations. I am trying to create to create a variable with the counts of all the other variables aggregated by month. Why does the first code not work:

df['bymonth']=df.groupby('Month').count()

But the following code does work:

bymonth=df.groupby(‘Month’).count()
Reply
#2
df.groupby('Month').count()
returns pandas serie with length equal to a number of unique items in df['Month'] column (these form index, values are items counts). So serie with aggregated values is usually "shorter" than length of original dataframe (unless there are only unique items in the Month column), and you cant assign it as a new column.
Reply
#3
(Mar-29-2017, 09:11 PM)zivoni Wrote:
 df.groupby('Month').count() 
returns pandas serie with length equal to a number of unique items in df['Month'] column (these form index, values are items counts). So serie with aggregated values is usually "shorter" than length of original dataframe (unless there are only unique items in the Month column), and you cant assign it as a new column.

Thank you for helping me understand the concept.
Reply


Forum Jump:

User Panel Messages

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