1

How to get number of groups in a groupby object in koalas ?

In pandas we can use ngroups, but this method is not implemented yet in koalas.

Suppose groupby object is called dfgroup.

Any idea ?

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
Ousen92i
  • 137
  • 1
  • 8

2 Answers2

1

You need to force execution of some function on that GroupBy object (type: databricks.koalas.groupby.DataFrameGroupBy), and from that you can get a length. For example, the dfgroup.size() will return databricks.koalas.series.Series on which you can call len (example is adapted from documentation):

>>> grouped = df.groupby(['Animal'])
>>> type(grouped.size())
<class 'databricks.koalas.series.Series'>
>>> len(grouped.size())
2
Alex Ott
  • 80,552
  • 8
  • 87
  • 132
0

in koalas

df1.groupby('School').first().pipe(len)
G.G
  • 639
  • 1
  • 5