TransWikia.com

What is the best way to color alternative rows but by values in a particular column of a dataset?

Mathematica Asked on January 23, 2021

Consider the following dataset as an example (the actual dataset is very large and has more columns so I am looking for an efficient method):

dataset=Dataset@{<|"Order Date" -> DateObject[{2020, 8, 22}],
   "Order ID" -> 4730765012|>, <|"Order Date" -> 
   DateObject[{2020, 8, 22}], 
  "Order ID" -> 8937641014|>, <|"Order Date" -> 
   DateObject[{2020, 8, 15}], 
  "Order ID" -> 9135404107|>, <|"Order Date" -> 
   DateObject[{2020, 8, 15}], 
  "Order ID" -> 4712250343|>, <|"Order Date" -> 
   DateObject[{2020, 8, 13}], 
  "Order ID" -> 5001983763|>, <|"Order Date" -> 
   DateObject[{2020, 8, 6}], 
  "Order ID" -> 5353297194|>, <|"Order Date" -> 
   DateObject[{2020, 7, 29}], 
  "Order ID" -> 6426299731|>, <|"Order Date" -> 
   DateObject[{2020, 7, 29}], 
  "Order ID" -> 5764698413|>, <|"Order Date" -> 
   DateObject[{2020, 7, 20}], 
  "Order ID" -> 8186942570|>, <|"Order Date" -> 
   DateObject[{2020, 7, 13}], 
  "Order ID" -> 4606355149|>, <|"Order Date" -> 
   DateObject[{2020, 7, 13}], 
  "Order ID" -> 9823067506|>, <|"Order Date" -> 
   DateObject[{2020, 7, 13}], 
  "Order ID" -> 3911461456|>, <|"Order Date" -> 
   DateObject[{2020, 7, 2}], 
  "Order ID" -> 7521423655|>, <|"Order Date" -> 
   DateObject[{2020, 6, 25}], 
  "Order ID" -> 3518924827|>, <|"Order Date" -> 
   DateObject[{2020, 6, 25}], 
  "Order ID" -> 2442353835|>, <|"Order Date" -> 
   DateObject[{2020, 6, 19}], 
  "Order ID" -> 1881309788|>, <|"Order Date" -> 
   DateObject[{2020, 6, 12}], 
  "Order ID" -> 7351689222|>, <|"Order Date" -> 
   DateObject[{2020, 6, 12}], 
  "Order ID" -> 5789690245|>, <|"Order Date" -> 
   DateObject[{2020, 6, 6}], 
  "Order ID" -> 2384265857|>, <|"Order Date" -> 
   DateObject[{2020, 5, 30}], 
  "Order ID" -> 7631877155|>, <|"Order Date" -> 
   DateObject[{2020, 5, 30}], 
  "Order ID" -> 3294381217|>, <|"Order Date" -> 
   DateObject[{2020, 5, 24}], 
  "Order ID" -> 3981185468|>, <|"Order Date" -> 
   DateObject[{2020, 5, 19}], 
  "Order ID" -> 7534143054|>};

Now I am looking to color alternative rows but treating the consecutive rows with identical values in "Order Date" the same. One way to do this is by:

Dataset[Values@dataset[GroupBy["Order Date"]],Background->{{{None,LightBlue}}},MaxItems->{All,All}]

But the issue is, this changes the structure of the dataset internally by introducing explicit groupings. I just want an appearance change rather than an actual change on the dataset.

Also if the target column is not sorted, this gathers the data which might not be desirable in a general setting.

One Answer

Let data be your list of associations, then

lengths = Length /@ SplitBy[data, #["Order Date"] &];
colors = Flatten@Riffle[
    ConstantArray[White, #] & /@ lengths[[1 ;; ;; 2]],
    ConstantArray[LightBlue, #] & /@ lengths[[2 ;; ;; 2]]
    ];
Dataset[data, Background -> {colors}, MaxItems -> All]

Mathematica graphics

Answered by C. E. on January 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP