ValueError: operands could not be broadcast together with shapes (7410,) (3,)
ValueError: operands could not be broadcast together with shapes (7410,) (3,)
I have a df in below format:
Priority Mined_Category server date_reported Count Zscore_Volume
1 - Critical Memory issue xxxxxx111 2018-07-11 1 nan
1 - Critical Memory issue xxxxxx111 2018-08-11 1 nan
1 - Critical Memory issue yyyyyy195 2018-07-06 1 1.71
1 - Critical Memory issue yyyyyy195 2018-07-08 1 1.71
2 - High Memory issue abcabcabcba1410 2018-08-21 1 nan
my aim is to replace nan with 100 whenever Priority Mined_Category and Server groupby count is 1 and replace nan with 1000 whenever Priority Mined_Category and Server groupby count is >1
Priority
Mined_Category
Server
Priority
Mined_Category
Server
I tried below code:
> df_aggegrate_Volume.loc[(df_aggegrate_Volume.groupby(["Priority","Mined_Category","server"]).count()>1)&(df_aggegrate_Volume['Zscore_Volume'].isnull()) ,"Zscore_Volume"]= -100
but I get below error:
ValueError: operands could not be broadcast together with shapes
(7410,) (3,)
1 Answer
1
Need GroupBy.transform for return Series with same size as original df filled by aggregate values:
GroupBy.transform
Series
df
m1 = (df_aggegrate_Volume.groupby(["Priority","Mined_Category","server"])["server"]
.transform('count')>1)
m2 = df_aggegrate_Volume['Zscore_Volume'].isnull()
df_aggegrate_Volume.loc[m1 & m2 ,"Zscore_Volume"]= -100
print (df_aggegrate_Volume)
Priority Mined_Category server date_reported Count
0 1 - Critical Memory issue xxxxxx111 2018-07-11 1
1 1 - Critical Memory issue xxxxxx111 2018-08-11 1
2 1 - Critical Memory issue yyyyyy195 2018-07-06 1
3 1 - Critical Memory issue yyyyyy195 2018-07-08 1
4 2 - High Memory issue abcabcabcba1410 2018-08-21 1
Zscore_Volume
0 -100.00
1 -100.00
2 1.71
3 1.71
4 NaN
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.