group-by

How can I group this array of hashes?

亡梦爱人 提交于 2019-12-20 09:09:47
问题 I have this array of hashes: - :name: Ben :age: 18 - :name: David :age: 19 - :name: Sam :age: 18 I need to group them by age , so they end up like this: 18: - :name: Ben :age: 18 - :name: Sam :age: 18 19: - :name: David :age: 19 I tried doing it this way: array = array.group_by &:age but I get this error: NoMethodError (undefined method `age' for {:name=>"Ben", :age=>18}:Hash): What am I doing wrong? I'm using Rails 3.0.1 and Ruby 1.9.2 回答1: The &:age means that the group_by method should

Python pandas: Add a column to my dataframe that counts a variable

僤鯓⒐⒋嵵緔 提交于 2019-12-20 08:40:36
问题 I have a dataframe 'gt' like this: org group org1 1 org2 1 org3 2 org4 3 org5 3 org6 3 and I would like to add column 'count' to gt dataframe to counts number member of the groups, expected results like this: org group count org1 1 2 org2 1 2 org3 2 1 org4 3 3 org5 3 3 org6 3 3 I know how to do it per one item of the group, but do not know how to make the count repeated for all of the group items, here is the code I have used: gtcounts = gt.groupby('group').count() Can anybody help? 回答1: Call

Pandas: Group by calendar-week, then plot grouped barplots for the real datetime

谁说我不能喝 提交于 2019-12-20 08:39:39
问题 EDIT I found a quite nice solution and posted it below as an answer. The result will look like this: Some example data you can generate for this problem: codes = list('ABCDEFGH'); dates = pd.Series(pd.date_range('2013-11-01', '2014-01-31')); dates = dates.append(dates) dates.sort() df = pd.DataFrame({'amount': np.random.randint(1, 10, dates.size), 'col1': np.random.choice(codes, dates.size), 'col2': np.random.choice(codes, dates.size), 'date': dates}) resulting in: In [55]: df Out[55]: amount

hive sql find the latest record

馋奶兔 提交于 2019-12-20 08:36:16
问题 the table is: create table test ( id string, name string, age string, modified string) data like this: id name age modifed 1 a 10 2011-11-11 11:11:11 1 a 11 2012-11-11 12:00:00 2 b 20 2012-12-10 10:11:12 2 b 20 2012-12-10 10:11:12 2 b 20 2012-12-12 10:11:12 2 b 20 2012-12-15 10:11:12 I want to get the latest record(include every colums id,name,age,modifed) group by id,as the data above,the correct result is: 1 a 11 2012-11-11 12:00:00 2 b 20 2012-12-15 10:11:12 I do like this: insert

Is there any workaround for sorting on multiValued field?

北城余情 提交于 2019-12-20 07:47:59
问题 Sorting can be done on the "score" of the document, or on any multiValued="false" indexed="true" field provided that field is either non-tokenized (ie: has no Analyzer) or uses an Analyzer that only produces a single Term (ie: uses the KeywordTokenizer) docs:- http://wiki.apache.org/solr/CommonQueryParameters#sort My original schema is (You can consider the following is a GROUP-BY) :- products (id, unique) users who make some comments(multiValued) last_comment_date for each user (multiValued,

midterm solution around query in SQL

匆匆过客 提交于 2019-12-20 07:29:53
问题 This is the database: EMPLOYEE (fmane, minit, lname, ssn, birthdate, address, sex, salary, superssn, dno) KEY: ssn DEPARTMENT (dname, dnumber, mgrssn, mgrstartdate) KEY: dnumber. PROJECT (pname, pnumber, plocation, dnum) KEY: pnumber. WORKS_ON (essn, pno, hours) KEY: (essn, pno) DEPENDENT (essn, dependent-name, sex, bdate, relationship) KEY: (essn, dependent-name) The question asked is... Give the last name and SSN of the unmarried employees who work on two or more projects. SELECT e.Lname, e

how to group by and count using MySQL

二次信任 提交于 2019-12-20 07:23:47
问题 I have data which looks like this: ID post_author post_title guid 3309 21 Should somebody not yet on SQL 2008 wait for SQL 2008 R2, since it's near release? http://sql.stackexchange.com/questions/379/should-somebody-not-yet-on-sql-2008-wait-for-sql-2008-r2-since-its-near-release 1695 429 How do we politely decline well meaning advice from the Grandmother? http://moms4mom.stackexchange.com/questions/1208/how-do-we-politely-decline-well-meaning-advice-from-the-grandmother 556 173 Books on how

Join multi tables and group by Id in LINQ

被刻印的时光 ゝ 提交于 2019-12-20 07:18:32
问题 I want to display a list product's name group by categoryId, this is my code: I want to my view display result: Desktop |_ PC HP - Red |_ PC Dell - Yellow |_ PC Asus - Red SmartPhone |_ Lumia 720 - Blue My GroupModel: public class GroupModel { public Category Categories { get; set; } public Product Products { get; set; } } My controller: List<Category> listCategories = new List<Category> { new Category {Id = 1, CateName = "SmartPhone"}, new Category {Id = 2, CateName = "Laptop"}, new Category

SQL Server 2005 error when grouping using subquery

好久不见. 提交于 2019-12-20 06:40:02
问题 Using SQL Server 2005 I'm trying to group based on a case statement with a subquery, but I'm getting an error ("Each GROUP BY expression must contain at least one column reference. "). I can work round it quite easily, but can anyone explain the error? I've got a column reference to #header.header. create table #header (header int) create table #detail (header int, detail int) insert into #header values (1) insert into #header values (2) insert into #header values (3) insert into #detail

SQL : How can I use sub query in a query with group by section?

笑着哭i 提交于 2019-12-20 06:33:01
问题 How can I use sub query in a query with group by section? I use SQL Server 2008 R2 AND Delphi 2010 I receive this error: Cannot perform an aggregate function on an expression containing an aggregate or a sub query. Like this query : select t1.sen, sum(t1.d1)as d1, sum(t1.d2)as d2, sum(t1.d1+t1.d2) as d_sum, Round((sum((1000*(t1.d1+t1.d2))/(9500-( select sum(t2.t_shab+t2.t_rooz) from tbl1 t2 where FCode=81 AND DCode=1 AND t2.sen<=t1.sen )))),1) as SSS from tbl1 t1 where FCode = 81 AND DCode =