Apr 17, 2018 23:35
@jezrael thanks! It did work. Though, I am trying to understand the concept for further usage. I also have column num_of_shares and I want to add aggregation of this column in the same way as you did for num_of_likes. I did s = cat_df.groupby('category')['num_of_likes','num_of_shares'].s‌​um() and added .assign(num_of_shares=lambda x: x['category'].map(s)). I got TypeError: 'DataFrame' object is not callable
Apr 17, 2018 23:35
@jezrael trying to implement. thx
Apr 17, 2018 23:35
@jezrael you have three instances of food in the sample data set, so the sum for num_of_likes would be 40=10+10+20. sorry for inaccurate explanations
Apr 17, 2018 23:35
@jezrael in your sample output, line 0 and line 2 would be combined because they belong to the same category food, the rest of the columns would be summed up
Apr 17, 2018 23:35
@jezrael I want to get a table category | total | depth=0 | depth>0 | num_of_likes where category - list of unique categories, total - number of posts in each category, depth=0 - number of root posts, depth>0 - number of comments, num_of_likes - total number of likes in the category.
Apr 17, 2018 23:35
@jezrael in this case, the categories are not unique values as in the original answer.
Apr 17, 2018 23:35
@jezrael so num_of_likes would be grouped within a category
Apr 17, 2018 23:35
oh, that is exactly how it works. But what I need is a table category | total | depth=0 | depth>0 | num_of_likes
Apr 17, 2018 23:35
@jezrael should I probably create a question with all the details
Apr 17, 2018 23:35
@jezrael yes, those are commented, I am not using them
Apr 17, 2018 23:35
@jezrael without value_counts and unstack? It doesn't show the df, only <pandas.core.groupby.SeriesGroupBy object at 0x7fe6c2006320>
Apr 17, 2018 23:35
@jezrael now I get AttributeError: 'list' object has no attribute 'sum' when trying to add .sum()
Apr 17, 2018 23:35
@jezrael prntscr.com/j6jvr3 hm
Apr 17, 2018 23:35
@jezrael still the same. pandas version is 0.22.0 I bet I am doing something wrong. I am trying to implement the answer's approach to a different dataframe, I probably missed something. This is the output I have pastebin.com/AKW37hiM - I want to sum the second column.
Apr 17, 2018 23:35
@jezrael I get KeyError: 2619288.7260000003
Apr 17, 2018 23:35
@jezrael thx, it worked for me, but it doesn't sum all the values in num_of_likes. For each 'like' I get separate line with depth=0, depth>0 etc. I'd want to sum all of them, I tried adding .sum() but didn't work.
Apr 17, 2018 23:35
@jezrael if I have third column 'num_of_likes' how do I include aggregation of this column into the table? I tried changing group_by to .groupby(cat_df['category'], cat_df['num_of_likes']) but I get TypeError: unhashable type: 'list'
Apr 17, 2018 23:35
makes much more sense now. How do I sort the table by 'total' column, should I use 'sort_values'?
Apr 17, 2018 23:35
I would highly appreciate the explanation.
Apr 17, 2018 23:35
Thanks! How do I sort it by total?
 

Lounge<C++>

Today we're daydreaming about C++26 reflection
Mar 4, 2014 00:32
Hi, what you would suggest for begginer in C++ ?