srowley

srowley

Simple count by group in Explorer

This feels like a dumb question but I am stuck. Using Explorer I want to count the number of rows for each group (groups may be defined by one column, or multiple columns).

I would think this would be:

alias Explorer.{Datasets, DataFrame}

Datasets.fossil_fuels()
|> DataFrame.group_by("country")
|> DataFrame.summarise(country: [:count])

And the result would be a data frame like:

#Explorer.DataFrame<
  Polars[222 x 2]
  country string ["AFGHANISTAN", "ALBANIA", "ALGERIA", "ANDORRA", "ANGOLA", ...]
  country_count integer [5, 5, 5, 5, 5, ...]
>

Instead I get this error:

%Inspect.Error{
  message: "got RuntimeError with message \"Polars(NotFound(\\\"country_count\\\"))\" 
  while inspecting %{__struct__: Explorer.DataFrame, data: %{__struct__:  
  Explorer.PolarsBackend.DataFrame, resource: #Reference<0.3895999949.566099982.184593>},
  dtypes: %{\"country\" => :string, \"country_count\" => :string}, groups: [], names: 
  [\"country\", \"country_count\"]}"
}

It works fine if I group and count by different variables, e.g.,

Datasets.fossil_fuels()
|> DataFrame.group_by("country")
|> DataFrame.summarise(total: [:count])

returns:

#Explorer.DataFrame<
  Polars[222 x 2]
  country string ["AFGHANISTAN", "ALBANIA", "ALGERIA", "ANDORRA", "ANGOLA", ...]
  total_count integer [5, 5, 5, 5, 5, ...]
>

but it seems odd that I have to summarise an arbitrary other variable. What am I missing?

Most Liked

NduatiK

NduatiK

From the docs:

When the dataframe has grouping variables, operations are performed per group

You can think of this as Explorer creating sub-dataframes using the grouping variable and applying operations on each group. But importantly, the sub-dataframe does not contain the variable any more (since it does not vary) and the variable cannot be used any longer.


                                 ┌───────────────────────────────────────────┐
  ┌──────────────────┐           │  Country 1    Country 2      Country N    │
  │                  │  group_by │ ┌─────────┐   ┌───────────┐  ┌──────────┐ │
  │   Fossil Fuels   ├───────────► │ Sub-DF  │   │ Sub-DF    │  │  Sub-DF  │ │
  │                  │           │ │         │   │           │  │          │ │
  └──────────────────┘           │ └─────────┘   └───────────┘  └──────────┘ │
                                 └───────────────────────────────────────────┘

Last Post!

srowley

srowley

As of version 0.3.0, Explorer.Series.summarise_with/2 is a bit more flexible:

alias Explorer.{Datasets, DataFrame, Series}

Datasets.fossil_fuels()
|> DataFrame.group_by("country")
|> DataFrame.summarise_with(&[country_count: Series.count(&1["country"])])

returns

#Explorer.DataFrame<
  Polars[222 x 2]
  country string ["AFGHANISTAN", "ALBANIA", "ALGERIA", "ANDORRA", "ANGOLA", ...]
  country_count integer [5, 5, 5, 5, 5, ...]
>

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement