Stream: helpdesk (published)

Topic: Random Selection From Within An Array Of Elements


view this post on Zulip QuBit (Mar 08 2021 at 22:31):

Hello,

I have

begin
Sessions = "Piano","Guitar","Harmonica"
Wall_E = DataFrame((ID = Any[1:10...], Cost = rand(1:50,10), Sessions = String["$Sessions" for i in 1:10]))
end

How can I create a random array of
elements in the dataframe?

Thank you,

view this post on Zulip Nils (Mar 09 2021 at 08:53):

This is weird code, why do you have begin and end here? Why are you passing a NamedTuple to the DataFrame constructor rather than just using the keywords? Why are you creating ID as Any[1:10...]?

To answer your question though, rand can sample from any collection, so you want:

DataFrame(ID = 1:10, Cost = rand(1:50,10), Sessions = rand(Sessions, 10))

view this post on Zulip QuBit (Mar 09 2021 at 09:01):

Hi @Nils ,
I am using Pluto.jl.
Also, I am new to using Julia and used
code that was posted here.
Thanks for your response, saved
me loads of time.

view this post on Zulip Nils (Mar 09 2021 at 09:43):

As a general aside, there's no need to always quote everyone's messages - they appear right above yours most of the time anyway, so it just adds unnecessary visual clutter.

Understood re Pluto, which I guess is where the begin ... end comes from. I would suggest to leave this out, and when you create minimum working examples to illustrate your issues just run them in a fresh Julia REPL session. If something then doesn't work in Pluto that's a Pluto specific issue and you should ask on the Pluto topic, but when just discussing DataFrame operations it's a bit of a distraction.

FInally, I would strongly encourage you to read through the DataFrames documentation, and work through the tutorial here: https://github.com/bkamins/Julia-DataFrames-Tutorial - I have a feeling that you'll be able to answer 95% of your own questions after doing so.

view this post on Zulip QuBit (Mar 09 2021 at 09:49):

Hi @Nils ,
Noted -- I will remove the code block designations
for Pluto.jl to not confuse the potential readers &
will not keep the quoted materials from any
respondents. Thank you for the resource will
explore it now!

Best,

view this post on Zulip Kwaku Oskin (Mar 09 2021 at 10:14):

I would also recommend to read series of blog posts in https://bkamins.github.io/ There you will usually find explanations of implementation details, close to real world examples, best practices and so on.

You may start from

and if you know pandas or R dataframes
https://bkamins.github.io/julialang/2020/09/25/comparisons.html

view this post on Zulip QuBit (Mar 09 2021 at 10:32):

Andrey Oskin

I will add this to my growing catalog of
Julia references. Thank you very much.

view this post on Zulip Michael Alexander (Mar 09 2021 at 15:18):

(deleted)


Last updated: Nov 06 2024 at 04:40 UTC