Home > database >  Julia select a group from a grouped Data Frame
Julia select a group from a grouped Data Frame

Time:11-20

I have the following DataFrame with 3 columns a, b, c. I grouped the DF by c

dfByC = groupby(df, [:C])

How can I select a group from dfByC for a certain value of c?

CodePudding user response:

Do:

dfByC[(the_value_you_have,)]

or

dfByC[(C=the_value_you_have,)]

or

dfByC[Dict(:C => the_value_you_have)]

In essence - you can do such selection by passing a Tuple, a NamedTuple or a dictionary.

The reason why it is not allowed to just write dfByC[the_value_you_have] is that you can also index GroupedDataFrame by integer, where you get the consecutive group, so we need some wrapper to disambiguate. Also if you groupby multiple columns you need some wrapper to keep them together.

Also group selection by grouping variable value is fast (so you can safely write a code where you do millions of such lookups and it will be efficient).

  • Related