Bottom Page

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()
Quote
#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.
smw10c likes this post
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to use bfill und groupby under conditions karlito 1 318 Dec-13-2019, 07:03 PM
Last Post: ibreeden
  Code improvement: groupby and operation on conditionals pythonidae 6 327 Dec-12-2019, 05:58 AM
Last Post: pythonidae
  itertuples, datetime, pandas, groupby, in range karlito 0 412 Nov-29-2019, 11:35 AM
Last Post: karlito
  Groupby([]).sum() Miscalculation mini_geek 1 278 Sep-16-2019, 12:43 AM
Last Post: Larz60+
  Groupby in pandas with conditional - add and subtract rregorr 2 1,450 Jul-12-2019, 05:17 PM
Last Post: rregorr
  Pandas segmenting groupby average brocq_18 0 630 Jul-11-2018, 10:54 AM
Last Post: brocq_18

Forum Jump:


Users browsing this thread: 1 Guest(s)