bill64k

bill64k

Calculate total percentage of a Explorer.Dataframe.group_by

My dataframe contains a knowledge graph showing a person’s proficiency level for a given skill (person, skill, proficiency). I would like to show the count of a person at a specific proficiency level and the percentage of this count to the total knowledge graph of the person.

For example: Edward Smarts has 129 total proficiencies, 79 of those proficiencies are at skill level == 4. The percentage of his proficiency skills at level 4 is 0.6124.

My working code uses multiple interim dataframes to bring the information together, but I am looking for a more optimized method. I made several attempts at summarise_by but could not get it to produce the results desired. Appreciate any suggestions? I am still learning the power of Livebook and the Explorer library.

total_profs_by_person =
  df
  |> DF.group_by(["person"])
  |> DF.summarise(total: count(person))
  |> DF.arrange(desc: total)
grouped = DF.group_by(df, ["person", "level"])

sme_count_by_person =
  grouped
  |> DF.summarise(count: count(level))
  |> DF.arrange(desc: count, asc: person)
  |> DF.filter(level == 4)
sme_percentage =
  DF.join(sme_count_by_person, total_profs_by_person)
  |> DF.mutate(percentage: count / total)
  |> DF.select(["person", "total", "level", "count", "percentage"])
  |> DF.arrange(desc: percentage)

Marked As Solved

imkleats

imkleats

It’s fairly common in data analysis to need to employ dummy variables or other ways to map categorical variables to an ordinal value (i.e. natural number), so even if you’re content with your first solution, I hope you don’t mind me sharing the solution to what’s blocking you from using pivot_wider.

You could add a column/series of ones to your initial dataframe. This new column is what you’d use for the values_from argument, and your existing skill column would be the names_from argument. Then you can group your data by [person] and perform a sum aggregation to obtain the count by skills.

Alternatively, if you want to start with the [person, skill] grouping with a count aggregation, your values_from argument would be whatever new column is holding the count. This would skip the dummy column of ones & the subsequent summation.

In any case, best of luck with your learning journey!

Also Liked

imkleats

imkleats

I would probably use a pivot to have a column per level. Then you’re simply dividing one column by the sum of a set of columns.

bill64k

bill64k

Is that a pivot_wider on level? Still trying to understand the differences of group_by and pivot_longer/wider. Can I do that on the original df without needing the interim group_bys?

imkleats

imkleats

Sorry for giving you a too-terse first answer, but I’m glad you followed up.

It would be pivot_wider to transform the enumerable values in your “levels” series into one column per distinct level.

I don’t think you can remove the [person, level] group by (bound to grouped), but the other (total_profs_by_person) becomes unnecessary. I think the order of operations doesn’t matter for whether the group_by is applied first or the pivot_wider is applied first, though.

(edit: when i say “doesn’t matter”, I mean the result wouldn’t be sensitive to changes. I’d probably do the group_by/count first because that reduces the size of the dataframe on which you’re applying the pivot_wider, so a minor memory optimization)

Last Post!

josevalim

josevalim

Creator of Elixir

Another idea is to use select+sum for counting: sum(select(level == 4, 1, 0)).

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
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

Other popular topics Top

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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
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 31494 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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

We're in Beta

About us Mission Statement