Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by without an aggregate function

I've seen a pandasql query like this:

df = pd.DataFrame({'A': [1, 2, 2], 'B': [3, 4, 5]})
sqldf('select * from df group by A', locals())

This gives:

   A  B
0  1  3
1  2  6

I find it really weird to have a group by without an aggregate function, but can anyone tell me which function is used on the aggregated columns to reduce multiple values into one?

like image 832
zoran119 Avatar asked Mar 20 '17 10:03

zoran119


People also ask

Can GROUP BY be used without aggregate functions?

GROUP BY without Aggregate Functions Although most of the times GROUP BY is used along with aggregate functions, it can still still used without aggregate functions — to find unique records.

Is aggregate function necessary with GROUP BY?

The GROUP BY clause is normally used along with five built-in, or "aggregate" functions. These functions perform special operations on an entire table or on a set, or group, of rows rather than on each row and then return one row of values for each group.

Can I use GROUP BY without aggregate function pandas?

Instead of using groupby aggregation together, we can perform groupby without aggregation which is applicable to aggregate data separately.

Can we use GROUP BY without HAVING?

A query with a having clause should also have a group by clause. If you omit group by, all the rows not excluded by the where clause return as a single group. Because no grouping is performed between the where and having clauses, they cannot act independently of each other.


1 Answers

It looks like the groupby method you're looking for is last():

df = pd.DataFrame({'A': [1, 2, 2], 'B': [3, 4, 5]})
df.groupby('A', as_index=False).last()

Output:

   A  B
0  1  3
1  2  5

I'm saying this assuming the 5 was a typo (see my comment above) and meant to be 6.

like image 153
Andrew L Avatar answered Sep 23 '22 06:09

Andrew L