group-by

Add GroupBy mean result as a new column in pandas

点点圈 提交于 2020-06-23 08:25:27
问题 I have a dataframe that gives upper and lower values of each indicator as follows df = pd.DataFrame( {'indicator': ['indicator 1', 'indicator 1', 'indicator 2', 'indicator 2'], 'year':[2014,2014,2015,2015], 'value type': ['upper', 'lower', 'upper', 'lower'], 'value':[12.3, 10.2, 15.4, 13.2] }, index=[1,2,3,4]) I want to remove the upper and lower values and replace that with the mean of two values. How can I do that? 回答1: You could groupby and transform by mean . df['value'] = df.groupby(

How to pivot a dataframe

余生长醉 提交于 2020-06-17 06:21:48
问题 What is pivot? How do I pivot? Is this a pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables. Even if they don't know that they are asking about pivot tables, they usually are. It is virtually impossible to write a canonical question and answer that encompasses all aspects of pivoting.... ... But I'm going to give it a go. The problem with existing questions and answers is that often the question is focused on a nuance that the OP has trouble

How to pivot a dataframe

六眼飞鱼酱① 提交于 2020-06-17 06:21:17
问题 What is pivot? How do I pivot? Is this a pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables. Even if they don't know that they are asking about pivot tables, they usually are. It is virtually impossible to write a canonical question and answer that encompasses all aspects of pivoting.... ... But I'm going to give it a go. The problem with existing questions and answers is that often the question is focused on a nuance that the OP has trouble

pandas DataFrame: aggregate values within blocks of repeating IDs

梦想的初衷 提交于 2020-06-16 19:56:45
问题 Given a DataFrame with an ID column and corresponding values column, how can I aggregate (let's say sum) the values within blocks of repeating IDs? Example DF: import numpy as np import pandas as pd df = pd.DataFrame( {'id': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'b'], 'v': np.ones(15)} ) Note that there's only two unique IDs, so a simple groupby('id') won't work. Also, the IDs don't alternate/repeat in a regular manner. What I came up with was to recreate the

pandas DataFrame: aggregate values within blocks of repeating IDs

戏子无情 提交于 2020-06-16 19:55:07
问题 Given a DataFrame with an ID column and corresponding values column, how can I aggregate (let's say sum) the values within blocks of repeating IDs? Example DF: import numpy as np import pandas as pd df = pd.DataFrame( {'id': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'b', 'a', 'b', 'b', 'b'], 'v': np.ones(15)} ) Note that there's only two unique IDs, so a simple groupby('id') won't work. Also, the IDs don't alternate/repeat in a regular manner. What I came up with was to recreate the

SQLite query to get table based on values of another table

岁酱吖の 提交于 2020-06-16 04:06:40
问题 I am not sure what title has to be here to correctly reflect my question, I can only describe what I want. There is a table with fields: id, name, city There are next rows: 1 John London 2 Mary Paris 3 John Paris 4 Samy London I want to get a such result: London Paris Total 2 2 John 1 1 Mary 0 1 Samy 1 0 So, I need to take all unique values of name and find an appropriate quantity for unique values of another field (city) Also I want to get a total quantity of each city Simple way to do it is

SQLite query to get table based on values of another table

时间秒杀一切 提交于 2020-06-16 04:06:32
问题 I am not sure what title has to be here to correctly reflect my question, I can only describe what I want. There is a table with fields: id, name, city There are next rows: 1 John London 2 Mary Paris 3 John Paris 4 Samy London I want to get a such result: London Paris Total 2 2 John 1 1 Mary 0 1 Samy 1 0 So, I need to take all unique values of name and find an appropriate quantity for unique values of another field (city) Also I want to get a total quantity of each city Simple way to do it is

SQLite query to get table based on values of another table

自古美人都是妖i 提交于 2020-06-16 04:06:01
问题 I am not sure what title has to be here to correctly reflect my question, I can only describe what I want. There is a table with fields: id, name, city There are next rows: 1 John London 2 Mary Paris 3 John Paris 4 Samy London I want to get a such result: London Paris Total 2 2 John 1 1 Mary 0 1 Samy 1 0 So, I need to take all unique values of name and find an appropriate quantity for unique values of another field (city) Also I want to get a total quantity of each city Simple way to do it is

Why does groupby operations behave differently

好久不见. 提交于 2020-06-15 18:59:30
问题 When using pandas groupby functions and manipulating the output after the groupby, I've noticed that some functions behave differently in terms of what is returned as the index and how this can be manipulated. Say we have a dataframe with the following information: Name Type ID 0 Book1 ebook 1 1 Book2 paper 2 2 Book3 paper 3 3 Book1 ebook 1 4 Book2 paper 2 if we do df.groupby(["Name", "Type"]).sum() we get a DataFrame : ID Name Type Book1 ebook 2 Book2 paper 4 Book3 paper 3 which contains a

r dplyr group_by values collapse and paste

£可爱£侵袭症+ 提交于 2020-06-12 07:24:06
问题 I have a data set that looks like this Id Subject Date Vitals Value 10 John 2001-05-29 HeartRate 65 10 John 2001-05-29 HeartRate 68 10 John 2001-05-29 BP-Arterial 48 10 John 2001-05-29 PulseRate 64 34 Pete 2005-08-15 HeartRate 68 34 Pete 2005-08-15 BP-Arterial 56 10 John 2004-09-25 HeartRate 65 10 John 2004-09-25 BP-Arterial 64 10 John 2004-09-25 PulseRate 63 34 Pete 2007-07-21 BP-Arterial 68 34 Pete 2007-07-21 PulseRate 56 I want to do two things, 1) group by Vitals. 2) Count the number of