aggregate

data.frame Group By column [duplicate]

谁说我不能喝 提交于 2019-12-17 06:28:37
问题 This question already has answers here : How to sum a variable by group (13 answers) Closed 2 years ago . I have a data frame DF. Say DF is: A B 1 1 2 2 1 3 3 2 3 4 3 5 5 3 6 Now I want to combine together the rows by the column A and to have the sum of the column B. For example: A B 1 1 5 2 2 3 3 3 11 I am doing this currently using an SQL query with the sqldf function. But for some reason it is very slow. Is there any more convenient way to do that? I could do it manually too using a for

Aggregate multiple columns at once [duplicate]

萝らか妹 提交于 2019-12-17 06:08:45
问题 This question already has answers here : Aggregate / summarize multiple variables per group (e.g. sum, mean) (6 answers) Closed 2 years ago . I have a data-frame likeso: x <- id1 id2 val1 val2 val3 val4 1 a x 1 9 2 a x 2 4 3 a y 3 5 4 a y 4 9 5 b x 1 7 6 b y 4 4 7 b x 3 9 8 b y 2 8 I wish to aggregate the above by id1 & id2. I want to be able to get the means for val1, val2, val3, val4 at the same time. How do i do this? This is what i currently have but it works just for 1 column: agg <-

How to select the first and last row within a grouping variable in a data frame?

戏子无情 提交于 2019-12-17 01:03:29
问题 How can i select the first and last row for each unique id in the following dataframe? tmp <- structure(list(id = c(15L, 15L, 15L, 15L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 24L, 24L, 24L, 24L), d = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), gr = c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L), mm = c(3.4, 4.9, 4.4, 5.5, 4, 3.8, 4, 4.9, 4.6, 2.7, 4, 3, 3, 2, 4, 2), area = c(1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L)), .Names = c

How to select the first and last row within a grouping variable in a data frame?

谁说我不能喝 提交于 2019-12-17 01:03:00
问题 How can i select the first and last row for each unique id in the following dataframe? tmp <- structure(list(id = c(15L, 15L, 15L, 15L, 21L, 21L, 22L, 22L, 22L, 23L, 23L, 23L, 24L, 24L, 24L, 24L), d = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), gr = c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L), mm = c(3.4, 4.9, 4.4, 5.5, 4, 3.8, 4, 4.9, 4.6, 2.7, 4, 3, 3, 2, 4, 2), area = c(1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L)), .Names = c

Multiple aggregations of the same column using pandas GroupBy.agg()

烂漫一生 提交于 2019-12-17 00:45:13
问题 Is there a pandas built-in way to apply two different aggregating functions f1, f2 to the same column df["returns"] , without having to call agg() multiple times? Example dataframe: import pandas as pd import datetime as dt pd.np.random.seed(0) df = pd.DataFrame({ "date" : [dt.date(2012, x, 1) for x in range(1, 11)], "returns" : 0.05 * np.random.randn(10), "dummy" : np.repeat(1, 10) }) The syntactically wrong, but intuitively right, way to do it would be: # Assume `f1` and `f2` are defined

MySQL学习之SQL函数

血红的双手。 提交于 2019-12-16 22:53:11
目录 SQL函数 Aggregate函数 Scalar函数 执行函数 获取函数返回值 SQL函数 SQL 函数 任何一个数据库系统都内置了数量相当可观的又非常实用的小函数 这些函数可以根据实现功能的不同划分为不同的类,当然,除了很明显的日期时间和字符串函数两大类外 可以把这些函数归纳为两大类: Aggregate 函数 和 Scalar 函数 Aggregate函数 avg() count() max() min() sum() 聚合函数中的 distinct SQL Aggregate 函数 SQL Aggregate 函数用于计算从列中取得的值,并返回一个单一的值 常用的 Aggregate 函数 ( 聚合函数 ) 有: 函数 说明 AVG ( ) 返回平均值 COUNT ( ) 返回行数 MAX ( ) 返回最大值 MIN ( ) 返回最小值 SUM ( ) 返回总和 # 聚合函数:max() / min() / sum() / avg() / count() avg ( ) 忽略列值为 NULL 的行 count ( ) 根据参数的不同, COUNT ( ) 大致有三种用法 1. COUNT ( column_name ) COUNT ( column_name ) 函数返回指定列的值的数目, NULL 值除外 SELECT COUNT ( column_name )

Collapse / concatenate / aggregate a column to a single comma separated string within each group

倖福魔咒の 提交于 2019-12-16 18:13:10
问题 I want to aggregate one column in a data frame according to two grouping variables, and separate the individual values by a comma. Here is some data: data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = rep(1:2, 3), C = c(5:10)) data # A B C # 1 111 1 5 # 2 111 2 6 # 3 111 1 7 # 4 222 2 8 # 5 222 1 9 # 6 222 2 10 "A" and "B" are grouping variables, and "C" is the variable that I want to collapse into a comma separated character string. I have tried: library(plyr) ddply(data, .(A,B),

Sum values in each group with a loop

五迷三道 提交于 2019-12-14 03:33:42
问题 I have a while loop that gives this result: Userid Point 1 10 1 15 2 5 2 10 3 8 3 2 How can I sum the userid points and output with highest number first, like this: Userid Point 1 25 2 20 3 10 Is there any "foreach", "for" or any other method that can accomplish such result? The code: include ('variables.php'); //Fetch data from matchdata table $q = "SELECT userid, matchid, homescore, awayscore FROM predictiondata ORDER BY userid ASC"; $r = mysqli_query($mysqli, $q); while ($row = mysqli

Printing all rows in each level of MultiIndex pd.DataFrame in one row

我的梦境 提交于 2019-12-14 03:25:44
问题 I have a dataframe which was converted into a multiIndex dataframe after doing groupby() and aggregation. In[1]: mydata = [['Team1', 'Player1', 'idTrip13', 133], ['Team2', 'Player333', 'idTrip10', 18373], ['Team3', 'Player22', 'idTrip12', 17338899], ['Team2', 'Player293','idTrip02', 17656], ['Team3', 'Player20', 'idTrip11', 1883], ['Team1', 'Player1', 'idTrip19', 19393]] df = pd.DataFrame(mydata, columns = ['team', 'player', 'trips', 'time']) df Out[1]: team player trips time 0 Team1 Player1

How to use Linq aggregate with single quotes

我是研究僧i 提交于 2019-12-14 02:26:32
问题 Let's assume that I have a collection of strings, like so: IList<string> myList = new List<string>(){"one", "two", "three"}; Using myList.Aggregate I would like to end up with "'one', 'two', 'three'" (including single quotes) Does someone have a sleak way of doing this, only using the Aggregate function? I was thinking something in the lines of myList.Aggregate((increment, seed) => string.Concat(increment, ", ", seed)); but that's only half the solution. 回答1: Any reason to use Aggregate