pandas - Set column name for size() -
i'm trying rename size()
column shown here this:
x = monthly.copy() x["size"] = x\ .groupby(["sub_acct_id", "clndr_yr_month"]).transform(np.size)
but i'm getting is
valueerror: wrong number of items passed 15, placement implies 1
why not working dataframe?
if simple print copy:
x = monthly.copy() print x
this how table looks like:
sub_acct_id clndr_yr_month 12716d 201601 219 201602 265 12716g 201601 221 201602 262 12716k 201601 181 201602 149 ...
what try accomplish set name of column:
sub_acct_id clndr_yr_month size 12716d 201601 219 201602 265 12716g 201601 221 201602 262 12716k 201601 181 201602 149 ...
you need:
x["size"] = x.groupby(["sub_acct_id", "clndr_yr_month"])['sub_acct_id'].transform('size')
sample:
df = pd.dataframe({'sub_acct_id': ['x', 'x', 'x','x','y','y','y','z','z'] , 'clndr_yr_month': ['a', 'b', 'c','c','a','b','c','a','b']}) print (df) clndr_yr_month sub_acct_id 0 x 1 b x 2 c x 3 c x 4 y 5 b y 6 c y 7 z 8 b z df['size'] = df.groupby(['sub_acct_id', 'clndr_yr_month'])['sub_acct_id'].transform('size') print (df) clndr_yr_month sub_acct_id size 0 x 1 1 b x 1 2 c x 2 3 c x 2 4 y 1 5 b y 1 6 c y 1 7 z 1 8 b z 1
another solution aggregating output:
df = df.groupby(['sub_acct_id', 'clndr_yr_month']).size().reset_index(name='size') print (df) sub_acct_id clndr_yr_month size 0 x 1 1 x b 1 2 x c 2 3 y 1 4 y b 1 5 y c 1 6 z 1 7 z b 1
Comments
Post a Comment