Aurel

Aurel

Equivalent of panda commands in Explorer

Hello :wave:

I am playing with Explorer coming from pandas, and have some difficulties finding the right way to do some of my usual workflow steps.

Reading data

When using pandas in Python, I use a lot of .loc[] and .iloc[] calls to access data.

What is the idiomatic way to access data in Explorer ?
The closest approach I could find is to grab the underlying series and the ask the value by index (which sounds like a mix of loc and iloc to me) for ex.:

df["year"][0]

but it doesn’t feel very natural.

Cleaning data

Most of the time when I work in pandas, the first step in the notebook is to clean the database before processing.

Does anybody know how to do it in Explorer ?
More precisely I look for the equivalent of the following commands:

  • Drop invalid columns (columns with only NaN or nils), equivalent to panda’s df.dropna(how='all', axis=1)
  • Same thing for invalid rows: df.dropna(how='all', axis=0
  • Data replacement with fillna : replace all NaN values with a given value df.fillna(0) or do it by column df.fillna({'col1': 0, 'col2': 3}) etc.
  • conversion from string to integer: an equivalent to pandas.to_numeric (with coercion of invalid values) would be useful, and equivalents to the “str methods” (to do some replace in the string before parsing them to numbers) would also be useful

If anybody can help on these I would be grateful :smiley:

I understand that these are newby questions, but I could not figure them on my own… I’m also biased towards the “pandas way” since I’ve been using it for a long time so there are probably ways I just don’t see.

Kind regards,
Aurélien

Marked As Solved

billylanchantin

billylanchantin

I also came from Pandas so Explorer concepts were confusing at first.

First, Explorer is built on Polars rather than Pandas. So you’re already at a bit of a disadvantage if you’re trying to carry over your Pandas intuition. This guide may help a bit with that aspect: Coming from Pandas - Polars user guide.

As for the specific questions:

What is the idiomatic way to access data in Explorer?

EDIT: left this section out at first.

This is IMO the biggest mindset shift in transferring from Pandas to Polars. In Polars, accessing data like Pandas does with .loc[] and .iloc[] is discouraged. I suggest reading over that “Coming from Pandas” tutorial to better understand why. There’s a section specifically for selecting data.

The thinking is roughly this: it’s better to try and express what data you want though the built-in functions that are available. Those functions are designed with fast querying/manipulating in mind.

For example, selecting data by its index isn’t usually what you want to do because the index of a particular row is usually incidental. It’ll be better to try and access it by its properties through filtering. E.g. I want the row where name == "Billy", not where index == 5. Filtering by name is just as fast as filtering by index, but filtering by name also works regardless of the row order.

If you really do need data at a specific index, you can do it just like you’ve described. But the Polars philosophy discourages this access pattern in favor of filtering/selecting for a good reason.

Drop columns with nils

I don’t think we have a specific function. This should work:

require Explorer.DataFrame, as: DF
require Explorer.Series, as: S

df = DF.new(a: [1, 2, 3], b: [4, nil, 6])
# #Explorer.DataFrame<
#   Polars[3 x 2]
#   a s64 [1, 2, 3]
#   b s64 [4, nil, 6]
# >

columns_with_nils =
  df
  # Benefit of Polars: this step happens in parallel.
  |> DF.summarise(for(col <- across(), do: {col.name, nil_count(col) > 0}))
  |> DF.pivot_longer(df.names, names_to: "column", values_to: "has_nil")
  |> DF.filter(has_nil)
  |> DF.pull("column")
  |> S.to_list()
  #=> ["b"]

DF.discard(df, columns_with_nils)
# #Explorer.DataFrame<
#   Polars[3 x 2]
#   a s64 [1, 2, 3]
# >

Drop rows with nils

Replace nils:

For multiple columns at once, do:

col_to_value = %{"a" => 1, "b" => 2}

DF.mutate(df, for col <- across() do
  {col.name, fill_missing(col, ^col_to_value[col.name])}
end)
# #Explorer.DataFrame<
#   Polars[3 x 2]
#   a s64 [1, 2, 3]
#   b s64 [4, 2, 6]
# >

Conversion from string to integer

I’m not as sure about this one. I’d need to know more about what you’re trying to accomplish specifically.


I understand that these are newby questions, but I could not figure them on my own

Newby questions are welcome here :slight_smile:

I’m also biased towards the “pandas way” since I’ve been using it for a long time

Related: try out Polars for your Python work. I heavily prefer it these days.

Also Liked

Aurel

Aurel

Wow thanks a lot for the detailed response :heart:

I obviously need to change my mindset, especially the “mask → filter → mask → filter…” logic inherited from numpy and pandas, but I’ve been using them for a long time so this is not easy…

I will also try to learn how to use across() which you use in your examples.

Regards, Aurélien

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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

We're in Beta

About us Mission Statement