group-by

Get only the first and last rows of each group with pandas

╄→尐↘猪︶ㄣ 提交于 2020-01-30 05:32:05
问题 Iam newbie in python. I have huge a dataframe with millions of rows and id. my data looks like this: Time ID X Y 8:00 A 23 100 9:00 B 24 110 10:00 B 25 120 11:00 C 26 130 12:00 C 27 140 13:00 A 28 150 14:00 A 29 160 15:00 D 30 170 16:00 C 31 180 17:00 B 32 190 18:00 A 33 200 19:00 C 34 210 20:00 A 35 220 21:00 B 36 230 22:00 C 37 240 23:00 B 38 250 I want to sort the data on id and time. The expected result what I looking for like this" Time ID X Y 8:00 A 23 100 13:00 A 28 150 14:00 A 29 160

MYSQL fill group by “gaps”

允我心安 提交于 2020-01-30 02:29:33
问题 I´m trying to fill the gaps after using group by using an aux table, can you help? aux table to deal with days with no orders date quantity 2014-01-01 0 2014-01-02 0 2014-01-03 0 2014-01-04 0 2014-01-05 0 2014-01-06 0 2014-01-07 0 group by result from "orders" table date quantity 2014-01-01 7 2014-01-02 1 2014-01-04 2 2014-01-05 3 desired result joining "orders" table with "aux table" date quantity 2014-01-01 7 2014-01-02 1 2014-01-03 0 2014-01-04 2 2014-01-05 3 2014-01-06 0 2014-01-07 0 回答1:

MYSQL fill group by “gaps”

♀尐吖头ヾ 提交于 2020-01-30 02:29:04
问题 I´m trying to fill the gaps after using group by using an aux table, can you help? aux table to deal with days with no orders date quantity 2014-01-01 0 2014-01-02 0 2014-01-03 0 2014-01-04 0 2014-01-05 0 2014-01-06 0 2014-01-07 0 group by result from "orders" table date quantity 2014-01-01 7 2014-01-02 1 2014-01-04 2 2014-01-05 3 desired result joining "orders" table with "aux table" date quantity 2014-01-01 7 2014-01-02 1 2014-01-03 0 2014-01-04 2 2014-01-05 3 2014-01-06 0 2014-01-07 0 回答1:

MySQL retrieve latest record for Group

左心房为你撑大大i 提交于 2020-01-28 07:13:40
问题 I have a social networking site and am struggling with a query. I have a posts table that holds all of the users posts and then a post_comments table that holds all comments on a post. I am trying to find the latest comment by post from post_comments table. The post_comments table has the following columns: post_comment_id, post_id, writer_user_id, post_comment_content, datetime I have grouped the results by post_id like so: SELECT * FROM post_comments GROUP BY post_id This almost does what I

MySQL retrieve latest record for Group

你说的曾经没有我的故事 提交于 2020-01-28 07:12:29
问题 I have a social networking site and am struggling with a query. I have a posts table that holds all of the users posts and then a post_comments table that holds all comments on a post. I am trying to find the latest comment by post from post_comments table. The post_comments table has the following columns: post_comment_id, post_id, writer_user_id, post_comment_content, datetime I have grouped the results by post_id like so: SELECT * FROM post_comments GROUP BY post_id This almost does what I

Get top 1 row of each group

可紊 提交于 2020-01-25 09:26:28
问题 I have a table which I want to get the latest entry for each group. Here's the table: DocumentStatusLogs Table |ID| DocumentID | Status | DateCreated | | 2| 1 | S1 | 7/29/2011 | | 3| 1 | S2 | 7/30/2011 | | 6| 1 | S1 | 8/02/2011 | | 1| 2 | S1 | 7/28/2011 | | 4| 2 | S2 | 7/30/2011 | | 5| 2 | S3 | 8/01/2011 | | 6| 3 | S1 | 8/02/2011 | The table will be grouped by DocumentID and sorted by DateCreated in descending order. For each DocumentID , I want to get the latest status. My preferred output:

MySQL - GROUP multiple rows while retaining differences

喜夏-厌秋 提交于 2020-01-25 08:36:05
问题 I have a series of complicated MySQL select queries / joins that produce results similar to this: |----|---------|----------|----------|---------------|---------------| | id | company | city | province | manager_email | staff_email | |----|---------|----------|----------|---------------|---------------| | 1 | aaa | toronto | ON | john@aaa.com | | | 1 | aaa | toronto | ON | | smith@aaa.com | | 2 | bbb | sudbury | ON | john@bbb.com | | | 3 | ccc | hamilton | ON | john@ccc.com | | | 3 | ccc |

Pandas groupby with identification of an element with max value in another column

旧街凉风 提交于 2020-01-25 03:57:04
问题 I have a dataframe with sales results of items with different pricing rules: import pandas as pd from datetime import timedelta df_1 = pd.DataFrame() df_2 = pd.DataFrame() df_3 = pd.DataFrame() # Create datetimes and data df_1['item'] = [1, 1, 2, 2, 2] df_1['date'] = pd.date_range('1/1/2018', periods=5, freq='D') df_1['price_rule'] = ['a', 'b', 'a', 'b', 'b'] df_1['sales']= [2, 4, 1, 5, 7] df_1['clicks']= [7, 8, 9, 10, 11] df_2['item'] = [1, 1, 2, 2, 2] df_2['date'] = pd.date_range('1/1/2018'

python pandas group by and aggregate columns

牧云@^-^@ 提交于 2020-01-25 02:59:24
问题 I am using panda version 0.23.0. I want to use data frame group by function to generate new aggregated columns using [lambda] functions.. My data frame looks like ID Flag Amount User 1 1 100 123345 1 1 55 123346 2 0 20 123346 2 0 30 123347 3 0 50 123348 I want to generate a table which looks like ID Flag0_Count Flag1_Count Flag0_Amount_SUM Flag1_Amount_SUM Flag0_User_Count Flag1_User_Count 1 2 2 0 155 0 2 2 2 0 50 0 2 0 3 1 0 50 0 1 0 here: Flag0_Count is count of Flag = 0 Flag1_Count is

Listing unique value counts per groups in pandas dataframe

白昼怎懂夜的黑 提交于 2020-01-24 19:32:09
问题 I am new to pandas and python. I am trying to group items by one column and list the information from the data frame per group. My dataframe: B C D E F 1 Honda USA 2000 Washington New 2 Honda USA 2001 Salt Lake Used 3 Ford Canada 2005 Washington New 4 Toyota USA 2010 Ney York Used 5 Honda USA 2001 Salt Lake Used 6 Honda Canada 2011 Salt Lake Crashed 7 Ford Italy 2014 Rome New I am trying to group my dataframe by column B and list how many C , D , E , F column values are in group B . For