imed-code

imed-code

Explorer DataFrame.new like Pandas (rows and columns)

Dear all,

I’m trying to use the new Library Explorer DataFrame as we have for Pandas (python) loading rows and columns arrays.

I have an output parsing HTML table and creating an array of columns and rows.

 def transform_request(body) do
    columns =
      body
      |> Floki.parse_document!()
      |> Floki.find("td.dxgvHeader_DevEx")
      |> Floki.find("tr")
      |> Enum.map(fn {"tr", [],
                      [
                        {"td", [{"style", "font-size:10pt;font-weight:normal;"}], [topic]},
                        {"td", [{"style", "width:1px;text-align:right;"}], [_topic_span]}
                      ]} ->
                        String.downcase(topic)
      end)

    rows =
      body
      |> Floki.parse_document!()
      |> Floki.find("tr.dxgvDataRow_DevEx")
      |> Floki.find("td")
      |> Enum.map(&parse_row/1)

    **DF.new(rows, columns)**

  end

  defp parse_row(
         {"td", [{"class", "dxgv"}, {"align", "right"}, {"style", "font-family:Arial;"}], [topic]}
       ) do
    #%{data: String.trim(topic)}
    String.trim(topic)
  end

  defp parse_row(_), do: ""

How can I load this Explorer DataFrame using columns and rows as an input? By the way, same code in pandas works very well e.g. pd.DataFrame(rows[1:], columns=rows[0])

Marked As Solved

NduatiK

NduatiK

Explorer’s accepts a list of maps in it’s DataFrame.new function.

columns = ["patient", "age", "gender"]

rows = [
  ["Sergio", 40, "M"],
  ["Maria", 12, "F"],
  ["João", 21, "M"],
  ["Mirela", 52, "F"]
]

rows
|> Enum.map(fn row ->
  columns
  |> Enum.zip(row)
  |> Enum.into(%{})
end)
|> Explorer.DataFrame.new()
#Explorer.DataFrame<
  Polars[4 x 3]
  age integer [40, 12, 21, 52]
  gender string ["M", "F", "M", "F"]
  patient string ["Sergio", "Maria", "João", "Mirela"]
>

By the way, it sounds like you will be pushing this into a DB and serving it over an API, unless you are using Explorer for other data manipulation, you might want to skip using DataFrames.

Last Post!

imed-code

imed-code

You’re right! It worked!

Thank you!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New

We're in Beta

About us Mission Statement