group-by

Group records by both month and year in Rails

∥☆過路亽.° 提交于 2019-12-21 03:39:18
问题 I'm on Ruby 1.9.2, Rails 3.0.x and I use MySQL DB. I have a Messages Model, where one can post new messages every day. I have an index action where I want to display these messages grouped by both month and year. This basically is used to filter messages. My query looks like this:- @active_msgs = Messages.where('expiry > ?', Time.now).order('created_at DESC') I used the group_by function in Rails, after referring to this post My Group By Query is:- @msgs_by_month = @active_msgs.all.group_by {

pandas, apply multiple functions of multiple columns to groupby object

时光怂恿深爱的人放手 提交于 2019-12-21 03:36:29
问题 I want to apply multiple functions of multiple columns to a groupby object which results in a new pandas.DataFrame . I know how to do it in seperate steps: by_user = lasts.groupby('user') elapsed_days = by_user.apply(lambda x: (x.elapsed_time * x.num_cores).sum() / 86400) running_days = by_user.apply(lambda x: (x.running_time * x.num_cores).sum() / 86400) user_df = elapsed_days.to_frame('elapsed_days').join(running_days.to_frame('running_days')) Which results in user_df being: However I

Python Pandas: Assign Last Value of DataFrame Group to All Entries of That Group

我与影子孤独终老i 提交于 2019-12-21 03:36:25
问题 In Python Pandas, I have a DataFrame. I group this DataFrame by a column and want to assign the last value of a column to all rows of another column. I know that I am able to select the last row of the group by this command: import pandas as pd df = pd.DataFrame({'a': (1,1,2,3,3), 'b':(20,21,30,40,41)}) print(df) print("-") result = df.groupby('a').nth(-1) print(result) Result: a b 0 1 20 1 1 21 2 2 30 3 3 40 4 3 41 - b a 1 21 2 30 3 41 How would it be possible to assign the result of this

GROUP BY or COUNT Like Field Values - UNPIVOT?

一世执手 提交于 2019-12-21 02:59:04
问题 I have a table with test fields, Example id | test1 | test2 | test3 | test4 | test5 +----------+----------+----------+----------+----------+----------+ 12345 | P | P | F | I | P So for each record I want to know how many Pass, Failed or Incomplete (P,F or I) Is there a way to GROUP BY value? Pseudo: SELECT ('P' IN (fields)) AS pass WHERE id = 12345 I have about 40 test fields that I need to somehow group together and I really don't want to write this super ugly, long query. Yes I know I

create nested objects in javascript like groupby in C#

柔情痞子 提交于 2019-12-21 02:26:13
问题 IList<Customer> Customers = flat.GroupBy(cust => new { cust.ReferenceNumber, cust.Name, cust.Address }) .Select(c => new Customer() { ReferenceNumber = c.Key.ReferenceNumber, Name = c.Key.Name, Address = c.Key.Address, Orders = c.Select(o => new Order() { OrderId = o.OrderId, ProductName = o.ProductName, Description = o.Description, Amount = o.Amount }).ToList() }).ToList() Is taking a flat list and converting it into a nested object possible in Javascript? A generic solution that is?? 回答1:

Aggregating data by timespan in MySQL

↘锁芯ラ 提交于 2019-12-20 23:48:22
问题 Basically I want is to aggregate some values in a table according to a timespan. What I do is, I take snapshots of a system every 15 minutes and I want to be able to draw some graph over a long period. Since the graphs get really confusing if too many points are shown (besides getting really slow to render) I want to reduce the number of points by aggregating multiple points into a single point by averaging over them. For this I'd have to be able to group by buckets that can be defined by me

group_by in rails by 2 or more attributes

旧街凉风 提交于 2019-12-20 10:29:43
问题 I have a @bunch of models returned as an array each model has the attributes - commentable_id and commentable_type (polymorphic association) I want to group the models by commentable, but if I do @bunch.group_by(&:commentable) it also fetches the commentable from the database, which is not needed. I can do @bunch.group_by(&:commentable_id) but this will cause some confusions since there can be several types of commentable models Is there a way to group_by commentable_id AND commentable_type ?

Group into a dictionary of elements

笑着哭i 提交于 2019-12-20 10:18:00
问题 Hi I have a list of element of class type class1 as show below. How do I group them into a Dictionary<int,List<SampleClass>> based on the groupID class SampleClass { public int groupID; public string someData; } I have done this way: var t =(from data in datas group data by data.groupID into dataGroups select dataGroups).ToDictionary(gdc => gdc.ToList()[0].groupID, gdc => gdc.ToList()); Is there a better way of doing thiss 回答1: It will be more efficient to replace: gdc => gdc.ToList()[0]

Simple sql to Linq query with group by and aggregate functions

主宰稳场 提交于 2019-12-20 09:47:14
问题 I'm fighting with linq trying to learn the syntax and I can't figure out how to do the following simple query SELECT DISTINCT user.firstname, user.lastname, COUNT(invoice.amount), SUM(invoice.amount) FROM company_user INNER JOIN user ON company_user.user_id = user.user_id INNER JOIN invoice ON invoice.user_id= invoice.user_id WHERE company_user.company_id = 1 GROUP BY user.firstname, user.lastname, GO Any help turning this into linq would be great. 回答1: The query you're after should be pretty

Using LINQ to group a list of objects

感情迁移 提交于 2019-12-20 09:35:14
问题 I have an object: public class Customer { public int ID { get; set; } public string Name { get; set; } public int GroupID { get; set; } } I return a list that may look like the following: List<Customer> CustomerList = new List<Customer>(); CustomerList.Add( new Customer { ID = 1, Name = "One", GroupID = 1 } ); CustomerList.Add( new Customer { ID = 2, Name = "Two", GroupID = 1 } ); CustomerList.Add( new Customer { ID = 3, Name = "Three", GroupID = 2 } ); CustomerList.Add( new Customer { ID = 4