qframe icon indicating copy to clipboard operation
qframe copied to clipboard

Grouping by multiple columns and creating a computed column

Open kishaningithub opened this issue 1 year ago • 1 comments
trafficstars

Would like to do the following logic (expressed as sql) using this library

select column1, column2, count(*) as count
from table
group by column1, column2

What i tried

dataFrame.
  GroupBy(groupby.Columns("column1", "column2")).
  Aggregate(qframe.Aggregation{
	  Fn: count,
	  Column: "count",
  })

Result

Aggregate: unknown column: "count"

kishaningithub avatar Feb 22 '24 07:02 kishaningithub

Hi, thanks for writing. I think it's an issue with count which doesn't actually require an existing column to aggregate over to exist. Normally, if it was a sum or similar, Column would have to exist, and that's the error that is triggered.

As a workaround you can specify an existing column in the aggregation and then alias it to whatever name you wish. Eg.

dataFrame.
  GroupBy(groupby.Columns("column1", "column2")).
  Aggregate(qframe.Aggregation{
	  Fn: "count",
	  Column: "NameOfAnExistingColumnAsWorkaround",
          As: "count",
  })

Pls note that "count" exists as a built in aggregation so you do not need to write it yourself. See https://pkg.go.dev/github.com/tobgu/qframe#example-QFrame-GroupByCount

Ultimately the check that fails now should be relaxed for "count". See https://github.com/tobgu/qframe/blob/master/grouper.go#L70-L79

tobgu avatar Feb 29 '24 22:02 tobgu