Nx.Explorer: How to get value of a cell?

Hello,

As I have a background in R the explorer package of Nx caught my interest.

In R I often need the value of a particular cell of a dataframe.
I have not found a function in Explorer.DataFrame yet that does the same.

I have seen take (an entire row) and pull (an entire column) though. So is it the best way to pull a series and then get the nth element of that series? Is this efficient?

On a site note: Are there downsides using Explorer.DataFrame as opposed to - let’s say - maps/structs?

1 Like

Hi @Nefcairon! Glad to hear it caught your interest. Yes, at the minute the best way to pull a series and then get the nth element of that series is to use pull and then index. But the Access protocol is implemented for Explorer.DataFrame and Explorer.Series. So you can access a given cell like so:

df = Explorer.Datasets.fossil_fuels() # as an example
df["country"][1] # gives us "ALBANIA"
# or without using `Access`:
df
|> Explorer.DataFrame.pull("country")
|> Explorer.Series.get(1)

It’s very efficient either way. In both cases, it’s almost entirely ‘Rust-side’ (as the default backend for Explorer is polars, which is a very, very fast Rust dataframe library).

What kind of downsides are you thinking of? Because the default backend is in Rust, it has to be compiled, which means having rustc installed. There’s work being done to provide precompiled binaries. And since it’s not just a list of maps/structs, you don’t have access to your typical Enum and Map functionality. It’s a different API. In return you get orders of magnitude faster operation and functionality that’s harder, slower, or more verbose with lists of maps/structs, like joins.

3 Likes

Hi @cigrainger,

Thanks a lot for your response.

What I was looking for was the Access protocol. That is clearly faster to type and similar to R.

Yea, I looked at the implementation details (down to Rust). I installed Rust via asdf and was very fast ready to go.

None, that’s why I asked :slight_smile:

My first “use case” will be reading rows of database tables into dataframes, transform them via Explorer.DataFrame API and feed them into Phoenix Liveview Surface components (sortable table and such). I know dataframes won’t be necessary therefore, but I love to work with them :wink:

1 Like