group-by

How to group by one column and sort the values of another column?

白昼怎懂夜的黑 提交于 2020-12-08 11:28:17
问题 Here is my dataframe import pandas as pd df = pd.DataFrame({'A': ['one', 'one', 'two', 'two', 'one'] , 'B': ['Ar', 'Br', 'Cr', 'Ar','Ar'] , 'C': ['12/15/2011', '11/11/2001', '08/30/2015', '07/3/1999','03/03/2000' ], 'D':[1,7,3,4,5]}) My goal is to group by column A and sort within grouped results by column B . Here is what I came up with: sort_group = df.sort_values('B').groupby('A') I was hoping that grouping operation would not distort order, but it does not work and also returns not a

Group By Row Value Difference [duplicate]

不想你离开。 提交于 2020-12-06 12:57:30
问题 This question already has answers here : Calculate difference between values in consecutive rows by group (3 answers) subtract value from previous row by group (2 answers) Closed 2 months ago . I have a table that has structure like: Year, Month, ValueA, ValueB, ValueC, etc.. I want to group the table by Year and Month, but aggregate based on the difference in column values. Year Month ValueA ValueB ValueC 2016 1 40 53 49 2017 2 29 31 26 2016 1 25 20 31 2017 2 22 30 29 I want to output a

python pandas groupby calculate change

折月煮酒 提交于 2020-11-27 01:52:03
问题 I want to calculate the value change by group. This is the python pandas dataframe df I have: Group | Date | Value A 01-02-2016 16 A 01-03-2016 15 A 01-04-2016 14 A 01-05-2016 17 A 01-06-2016 19 A 01-07-2016 20 B 01-02-2016 16 B 01-03-2016 13 B 01-04-2016 13 C 01-02-2016 16 C 01-03-2016 16 I want to calculate that for Group A, the values are going up, for Group B they are going down and for Group C they are not changing. I am not sure how to approach it, since in Group A the values initially