toodle

toodle

List operation `sort` in Explorer

I have an Explorer dataframe with a column that is a list type ({:list, {:s, 64}} ), and I’d like to mutate the dataframe to have a column that contains the sorted version of this list.

iex> df_with_list_col = DF.new(%{
       list_col: [[1, 5, 3], [1, 1, 2, 0]], 
       regular_col: ["regular", "column"]
     })
iex> DF.print(df_with_list_col)
"""
+-------------------------------------------+
| Explorer DataFrame: [rows: 2, columns: 2] |
+---------------------+---------------------+
|     regular_col     |      list_col       |
|      <string>       |     <list[s64]>     |
+=====================+=====================+
| regular             | 1                   |
|                     | 5                   |
|                     | 3                   |
+---------------------+---------------------+
| column              | 1                   |
|                     | 1                   |
|                     | 2                   |
|                     | 0                   |
+---------------------+---------------------+
"""

I wanted to reach for a Explorer.Series list op , since there appears to be what I want in Polars, but it seems to be missing from the Explorer API. I was expecting to be able to do something like:

iex> DF.mutate(df_with_list_col, list_col_sorted: sort(list_col))
     |> DF.print()
"""
+----------------------------------------------------------------------+
|             Explorer DataFrame: [rows: 2, columns: 3]                |
+---------------------+---------------------+--------------------------+
|     regular_col     |      list_col       |      list_col_sorted     |
|      <string>       |     <list[s64]>     |        <list[s64]>       |
+=====================+=====================+==========================+
| regular             | 1                   | 1                        |
|                     | 5                   | 3                        |
|                     | 3                   | 5                        |
+---------------------+---------------------+--------------------------+
| column              | 1                   | 0                        |
|                     | 1                   | 1                        | 
|                     | 2                   | 1                        |
|                     | 0                   | 2                        | 
+---------------------+---------------------+--------------------------+
"""

Is there perhaps a different recommended way of doing this? I suppose there is one hint here in this open issue, but I’m wondering if there is something much simpler and more obvious that I’m just missing.

First Post!

billylanchantin

billylanchantin

Hi, @toodle!

Unfortunately no, you’re not missing anything. Our support for the list column type is newer and we’ve not yet exposed all related Polars functionality.

Using the same method as the hint in the PR, this should work for now:

df_with_list_col
|> DF.explode(:list_col)
|> DF.sort_by(list_col)
|> DF.group_by(:regular_col)
|> DF.summarise(list_col: list_col)
|> DF.print()
# +-------------------------------------------+
# | Explorer DataFrame: [rows: 2, columns: 2] |
# +---------------------+---------------------+
# |     regular_col     |      list_col       |
# |      <string>       |     <list[s64]>     |
# +=====================+=====================+
# | column              | 0                   |
# |                     | 1                   |
# |                     | 1                   |
# |                     | 2                   |
# +---------------------+---------------------+
# | regular             | 1                   |
# |                     | 3                   |
# |                     | 5                   |
# +---------------------+---------------------+

Basically the current workaround is: explode → operate on exploded column → group_bysummarise.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement