toodle

toodle

I’d like to query an Explorer DataFrame containing a “select” column such that the output gives, for each row, the field/column that was pointed to by the “select” column. Example:

iex>DF.print(my_df)
+--------------------------------------------+
| Explorer DataFrame: [rows: 3, columns: 3]  |
+--------------+--------------+--------------+
|     col1     |     col2     |    select    |
|   <string>   |   <string>   |   <string>   |
+==============+==============+==============+
| a            | b            | col1         |
+--------------+--------------+--------------+
| c            | d            | col2         |
+--------------+--------------+--------------+
| e            | f            | col2         |
+--------------+--------------+--------------+

The output I want is:

#Explorer.Series<
  Polars[3]
  string ["a", "d", "f"]
>

This pops up while processing categorical data and is related to a use-case involving one-hot encoding that was discussed here.

I was hoping to be able to use syntax that is fairly short, something like

my_df[.., my_df["select"]]

…but I haven’t been able to get that working yet. What I have gotten working is:

defmodule Janky do
  def dynamic_select(dataframe) do
    # use mutate_with combined with Series.select
    DF.mutate_with(dataframe, fn df ->
      [selection: 
          Enum.reduce(
            Enum.reject(dataframe.names, fn x -> x == "select" end),
            "nil",
            fn x, acc ->
              this_column = S.equal(df["select"], x)
              S.select(this_column, df[x], acc)
            end
          )
      ]
    end)
    |> DF.pull("selection")
  end
end

iex> Janky.dynamic_select(my_df)
#Explorer.Series<
  Polars[3]
  string ["a", "d", "f"]
>

This seems like a lot of code to do something fairly straightforward—is there anything more sleek built into Explorer?

In Pandas, there used to be DataFrame.lookup(), which I guess has been replaced by something slightly more verbose. I feel like this type of dynamic selection is supported by dplyr too…

Anything built into Explorer (or on the roadmap) for this?

First Post!

billylanchantin

billylanchantin

For the two-column example, there’s this:

df["select"] |> S.equal("col1") |> S.select(df["col1"], df["col2"])

To generalize, this works:

Enum.reduce(df.names -- ["select"], df["select"], fn col, acc ->
  df["select"] |> S.equal(col) |> S.select(df[col], acc)
end)

It’s very close what you had. The overall approach does seem a bit wasteful – I’d love to do things lazily. But I tried a few other ideas and didn’t get anywhere.

As for the roadmap, I think exposing some fold-based expressions may help here:

Last Post!

toodle

toodle OP

Ah, I see, so without mutate_with, pretty much. Nice! that is a bit tidier, thanks! Unfortunately, as you mentioned, it does remove the ability to do things lazily.

Even on eager frames, I’m seeing that mutate_with makes things 3-4x faster, kind of interersting.

r.e. fold, that seems very relevant, although it appears like it requires pretty much the same amount of code as this example uses. I’m thinking more tight function calls (that maybe wrap fold or what we’ve written here). Seems like a type of horizontal aggregation, which maybe it’d be helpful for Explorer to have a small number of. Polars issue tracking horizontal aggregations.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews