group-by

Insert rows as a result of a groupby operation into the original dataframe

孤者浪人 提交于 2020-01-02 07:51:53
问题 For example, I have a pandas dataframe as follows: col_1 col_2 col_3 col_4 a X 5 1 a Y 3 2 a Z 6 4 b X 7 8 b Y 4 3 b Z 6 5 And I want to, for each value in col_1, add the values in col_3 and col_4 (and many more columns) that correspond to X and Z from col_2 and create a new row with these values. So the output would be as below: col_1 col_2 col_3 col_4 a X 5 1 a Y 3 2 a Z 6 4 a NEW 11 5 b X 7 8 b Y 4 3 b Z 6 5 b NEW 13 13 Also, there could be more values in col_1 that will need the same

Pandas enumerate groups in descending order

瘦欲@ 提交于 2020-01-02 07:25:23
问题 I've the following column: column 0 10 1 10 2 8 3 8 4 6 5 6 My goal is to find the today unique values (3 in this case) and create a new column which would create the following new_column 0 3 1 3 2 2 3 2 4 1 5 1 The numbering starts from length of unique values (3) and same number is repeated if current row is same as previous row based on original column. Number gets decreased as row value changes. All unique values in original column have same number of rows (2 rows for each unique value in

how does the groupby and count work in sql

爷,独闯天下 提交于 2020-01-02 07:08:27
问题 1> select browser,count(*) from logtest group by browser; +-----------+----------+ | browser | count(*) | +-----------+----------+ | Firefox 3 | 14 | | Unknown | 11 | +-----------+----------+ 2 rows in set 2> select browser,count(browser) from logtest group by browser; +-----------+----------------+ | browser | count(browser) | +-----------+----------------+ | Firefox 3 | 14 | | Unknown | 11 | +-----------+----------------+ 2 rows in set 3> select browser,count(browser) from logtest; +-------

How to find size of query result

不羁岁月 提交于 2020-01-02 07:04:11
问题 I have the following query in rails: records = Record.select('y_id, source') .where(:source => source, :y_id => y_id) .group(:y_id, :source) .having('count(*) = 1') I get the following output if I puts records : [#<Record source: "XYZ", y_id: 10000009>, #<Record source: "XYZ", y_id: 10000070>] This looks like there are 2 elements in the output array. But when I try to do records.size I get: {[10000009, "XYZ"]=>1, [10000070, "XYZ"]=>1} Why doesn't records.size print 2 when records is an array

How to find size of query result

别等时光非礼了梦想. 提交于 2020-01-02 07:04:05
问题 I have the following query in rails: records = Record.select('y_id, source') .where(:source => source, :y_id => y_id) .group(:y_id, :source) .having('count(*) = 1') I get the following output if I puts records : [#<Record source: "XYZ", y_id: 10000009>, #<Record source: "XYZ", y_id: 10000070>] This looks like there are 2 elements in the output array. But when I try to do records.size I get: {[10000009, "XYZ"]=>1, [10000070, "XYZ"]=>1} Why doesn't records.size print 2 when records is an array

group by day for the past 5 days

馋奶兔 提交于 2020-01-02 05:21:05
问题 I am trying to select the sum of an integer field for the past 5 days, and I need to group it for each day. I'm having a bit of issues figuring out the grouping. Here's my sql query so far: select sum(`amount_sale`) as total from `sales` where the_date >= unix_timestamp((CURDATE() - INTERVAL 5 DAY)) that works fine for generating the sum for all 5 days together, but I need to break this down so that it shows the sum for each of the past 5 days i.e: day 1 - $200 day 2- $500 day 3 - $20 etc.

oracle sql select syntax with GROUP BY and HAVING clause

不打扰是莪最后的温柔 提交于 2020-01-02 04:47:24
问题 I been going thru some of the sql syntax to study for the oracle sql exam, I found something rather confusing based on the official references, the select syntax is as follow : SELECT [ hint ] [ { { DISTINCT | UNIQUE } | ALL } ] select_list FROM { table_reference | join_clause | ( join_clause ) } [ , { table_reference | join_clause | (join_clause) } ] ... [ where_clause ] [ hierarchical_query_clause ] [ group_by_clause ] [ HAVING condition ] [ model_clause ] based on this you cannot have the

Loose index scan in Postgres on more than one field?

Deadly 提交于 2020-01-02 02:01:07
问题 I have several large tables in Postgres 9.2 (millions of rows) where I need to generate a unique code based on the combination of two fields, 'source' (varchar) and 'id' (int). I can do this by generating row_numbers over the result of: SELECT source,id FROM tablename GROUP BY source,id but the results can take a while to process. It has been recommended that if the fields are indexed, and there are a proportionally small number of index values (which is my case), that a loose index scan may

Artists Group_by nested attribute Order_date

旧城冷巷雨未停 提交于 2020-01-01 19:56:32
问题 I have created a functioning e-commerce platform where Members can buy songs. Everything works fine, But I would like to group all of my Orders in my Index Page by Month. Currently I am able to group each Album with its corresponding Artist, and each Ordered Song to its corresponding Album. But now I would like to group Orders by Month. How Can I Group Artists by the order_date in my Orders Table, So that everything is organized by Month? Ex. of what I've like to do Month 1 Orders Artist1

Rails 4 where,order,group,count include zero's - postgresql

我们两清 提交于 2020-01-01 19:25:29
问题 Here is my query: User.where("created_at >= ? AND created_at <=?", date1,date2).order('DATE(created_at) DESC').group("DATE(created_at)").count and I get output as: {Thu, 15 May 2014=>1} But I want to get output as 0 for the rest of the days. For ex {Thu, 15 May 2014=>1,Fri, 15 May 2014=>0} What I want to get is Users created in a date range , ordered and grouped by created_at and number of such Users for each day. When no users are there for a particular day it should return 0, which the