group-by

pandas aggregate dataframe returns only one column

南楼画角 提交于 2020-01-24 13:54:28
问题 Hy there. I have a pandas DataFrame (df) like this: foo id1 bar id2 0 8.0 1 NULL 1 1 5.0 1 NULL 1 2 3.0 1 NULL 1 3 4.0 1 1 2 4 7.0 1 3 2 5 9.0 1 4 3 6 5.0 1 2 3 7 7.0 1 3 1 ... I want to group by id1 and id2 and try to get the mean of foo and bar. My code: res = df.groupby(["id1","id2"])["foo","bar"].mean() What I get is almost what I expect: foo id1 id2 1 1 5.750000 2 7.000000 2 1 3.500000 2 1.500000 3 1 6.000000 2 5.333333 The values in column "foo" are exactly the average values (means)

pandas aggregate dataframe returns only one column

北战南征 提交于 2020-01-24 13:54:26
问题 Hy there. I have a pandas DataFrame (df) like this: foo id1 bar id2 0 8.0 1 NULL 1 1 5.0 1 NULL 1 2 3.0 1 NULL 1 3 4.0 1 1 2 4 7.0 1 3 2 5 9.0 1 4 3 6 5.0 1 2 3 7 7.0 1 3 1 ... I want to group by id1 and id2 and try to get the mean of foo and bar. My code: res = df.groupby(["id1","id2"])["foo","bar"].mean() What I get is almost what I expect: foo id1 id2 1 1 5.750000 2 7.000000 2 1 3.500000 2 1.500000 3 1 6.000000 2 5.333333 The values in column "foo" are exactly the average values (means)

Calculate sum of a column if the difference between consecutive rows meets a condition

时间秒杀一切 提交于 2020-01-24 13:33:38
问题 This is a continued question from the post Remove the first row from each group if the second row meets a condition Below is a sample dataset: df <- data.frame(id=c("9","9","9","5","5","4","4","4","4","4","20","20"), Date=c("11/29/2018","11/29/2018","11/29/2018","2/13/2019","2/13/2019", "6/15/2018","6/20/2018","8/17/2018","8/20/2018","8/23/2018","12/25/2018","12/25/2018"), Buyer= c("John","John","John","Maria","Maria","Sandy","Sandy","Sandy","Sandy","Sandy","Paul","Paul"), Amount= c("959",

MAX function used with GROUP BY clause

限于喜欢 提交于 2020-01-24 12:05:30
问题 I have database with name, salary and department of employees. I need a query for getting employee(s) with highest salaries in each department. Database: create table test( employee_name VARCHAR(255), department VARCHAR(255), salary INT ); Data: INSERT INTO test(employee_name, department, salary) VALUES ("John", "DepartmentA", 1500), ("Sarah","DepartmentA", 1600), ("Romel","DepartmentA", 1400), ("Victoria","DepartmentB", 1400), ("Maria", "DepartmentB", 1600); My tries: 1.1 WHERE MAX(salary) =

How to count and group query to get proper results?

 ̄綄美尐妖づ 提交于 2020-01-24 11:44:08
问题 I have a problem, please see my database: ------------------- | id | article_id | ------------------- | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 2 | | 6 | 3 | | 7 | 3 | | 8 | 3 | | 9 | 3 | | 10 | 3 | And I want to receive something like this (order by votes, from max to min): --------------------------- | id | article_id | votes | --------------------------- | 1 | 3 | 5 | | 2 | 1 | 3 | | 3 | 2 | 2 | Could you please help me to write proper sql query? 回答1: SELECT article_id, COUNT(article

How to count and group query to get proper results?

喜夏-厌秋 提交于 2020-01-24 11:44:05
问题 I have a problem, please see my database: ------------------- | id | article_id | ------------------- | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 2 | | 6 | 3 | | 7 | 3 | | 8 | 3 | | 9 | 3 | | 10 | 3 | And I want to receive something like this (order by votes, from max to min): --------------------------- | id | article_id | votes | --------------------------- | 1 | 3 | 5 | | 2 | 1 | 3 | | 3 | 2 | 2 | Could you please help me to write proper sql query? 回答1: SELECT article_id, COUNT(article

mysql query for grabbing multiple date ranges

*爱你&永不变心* 提交于 2020-01-24 10:31:05
问题 I seem to be having a bit of trouble coming up a query to achieve what I want. I have a table like the following.. | Date(TIMESTAMP) | Count | |---------------------|-------| | 2016-02-01 01:00:00 | 52 | | 2016-01-05 11:30:00 | 14 | | 2016-02-01 04:20:00 | 36 | | ... | ... | The table has about 40,000 rows. What I would like to do is grab the totals for multiple date ranges so I end up with the following... | Period | Total | |------------|-------| | All | 10245 | | Past year | 1401 | | Past

mysql query for grabbing multiple date ranges

筅森魡賤 提交于 2020-01-24 10:30:07
问题 I seem to be having a bit of trouble coming up a query to achieve what I want. I have a table like the following.. | Date(TIMESTAMP) | Count | |---------------------|-------| | 2016-02-01 01:00:00 | 52 | | 2016-01-05 11:30:00 | 14 | | 2016-02-01 04:20:00 | 36 | | ... | ... | The table has about 40,000 rows. What I would like to do is grab the totals for multiple date ranges so I end up with the following... | Period | Total | |------------|-------| | All | 10245 | | Past year | 1401 | | Past

SSRS Group By Parameter

一世执手 提交于 2020-01-24 06:09:14
问题 If I have a report parameter ( @Fruit ) that allows multiple selections how can I use the parameter as a column group in an SSRS Matrix? Typically, I would use the parameter in my query like the following: WHERE tbl.fruit In ( @Fruit ) Then I would use the column in the dataset as my group. However, in this case I need the full dataset returned. I can't filter the query, but I still want to only display the groups selected by the parameter. If my query returns Apples, Oranges and Bananas, but

SSRS Group By Parameter

☆樱花仙子☆ 提交于 2020-01-24 06:07:50
问题 If I have a report parameter ( @Fruit ) that allows multiple selections how can I use the parameter as a column group in an SSRS Matrix? Typically, I would use the parameter in my query like the following: WHERE tbl.fruit In ( @Fruit ) Then I would use the column in the dataset as my group. However, in this case I need the full dataset returned. I can't filter the query, but I still want to only display the groups selected by the parameter. If my query returns Apples, Oranges and Bananas, but