3
$\begingroup$

Below is a sample of the data I'm preprocessing. It's longer in both dimensions than what's shown. I already have a list of the string entries I want and now need grab the rows of numeric data associated with each.

{{"/SEB/AHU1/DischargeAirFlow.csv", 5593.62, 5593.62, 5593.62, 
  5530.92}, {"/SEB/AHU1/OutdoorAirFlow.csv", 4239.78, 4187.84, 
  4203.27, 4262.71}, {"/SEB/AHU1/VAV100/ZoneAirFlow.csv", 251., 243., 
  251., 251.}, {"/SEB/AHU1/VAV100/ZoneTemperature.csv", 69.9, 69.8, 
  69.8, 69.8}, {"/SEB/AHU1/VAV102/ZoneAirFlow.csv", 106., 87., 85., 
  85.}, {"/SEB/AHU1/VAV102/ZoneTemperature.csv", 70.1, 70.1, 70.1, 
  70.1}, {"/SEB/AHU1/VAV118/ZoneAirFlow.csv", 370., 372., 374., 
  372.}, {"/SEB/AHU1/VAV118/ZoneTemperature.csv", 69.4, 69.6, 69.4, 
  69.4}, {"/SEB/AHU1/VAV119/ZoneAirFlow.csv", 28., 26., 26., 
  32.}, {"/SEB/AHU1/VAV119/ZoneTemperature.csv", 70.1, 70.2, 70.2, 
  70.2}, {"/SEB/AHU1/VAV120/ZoneAirFlow.csv", 38., 0., 0., 
  32.}, {"/SEB/AHU1/VAV120/ZoneTemperature.csv", 70.7, 70.8, 70.7, 
  70.7}, {"/SEB/AHU1/VAV121/ZoneAirFlow.csv", 0., 33., 35., 
  35.}, {"/SEB/AHU1/VAV121/ZoneTemperature.csv", 71.1, 71.2, 71.2, 
  71.2}, {"/SEB/AHU1/VAV123A/ZoneAirFlow.csv", 269., 273., 222., 
  217.}, {"/SEB/AHU1/VAV123A/ZoneTemperature.csv", 70.6, 70.6, 70.6, 
  70.6}, {"/SEB/AHU1/VAV123B/ZoneAirFlow.csv", 314., 314., 262., 
  0.}, {"/SEB/AHU1/VAV127A/ZoneAirFlow.csv", 2144., 2153., 2157., 
  2153.}, {"/SEB/AHU1/VAV127A/ZoneTemperature.csv", 71.8, 71.8, 71.8, 
  71.8}, {"/SEB/AHU1/VAV127B/ZoneAirFlow.csv", 316., 312., 316., 
  312.}, {"/SEB/AHU1/VAV127B/ZoneTemperature.csv", 73.4, 73.3, 73.4, 
  73.4}, {"/SEB/AHU1/VAV129/ZoneAirFlow.csv", 48., 49., 49., 
  49.}, {"/SEB/AHU1/VAV129/ZoneTemperature.csv", 71.6, 71.7, 71.6, 
  71.6}, {"/SEB/AHU1/VAV131/ZoneAirFlow.csv", 34., 34., 35., 
  35.}, {"/SEB/AHU1/VAV131/ZoneTemperature.csv", 70.1, 70.1, 70.1, 
  70.1}}

Maybe it's best to convert everything to a Dataset first and select it that way but I rather only convert the smaller amount of data to a Dataset for further processing.

I tried a couple methods of using Cases and Select but was disappointed I couldn't get either to work and had to fall back on a Do loop. I was still left wondering if there was a better approach and wanted to learn it if anyone has any suggestions.

OldSchoolLoopFilter[data_] := Module[{arr, searchString},
  arr = {};
  searchStrings = columnsToGet; (* list of strings *)
  Do[
   If[
    StringContainsQ[data[[i, 1]], searchStrings], 
    AppendTo[arr, data[[i]]]
    ],
   {i, Length@data}
   ];
  arr
  ]
$\endgroup$
7
$\begingroup$

With lists as your input list of lists:

searchString="VAV11";

Select[StringContainsQ[searchString] @* First] @ lists
{{"/SEB/AHU1/VAV118/ZoneAirFlow.csv", 370., 372., 374., 372.}, 
 {"/SEB/AHU1/VAV118/ZoneTemperature.csv", 69.4, 69.6, 69.4, 69.4}, 
 {"/SEB/AHU1/VAV119/ZoneAirFlow.csv", 28., 26., 26., 32.}, 
 {"/SEB/AHU1/VAV119/ZoneTemperature.csv", 70.1, 70.2, 70.2, 70.2}} 

Alternatively, construct a Dataset from lists

ds = Dataset[Association[# -> {##2} & @@@ lists]]

enter image description here

and query using KeySelect:

KeySelect[StringContainsQ[searchString]] @ ds
(* ds[KeySelect[StringContainsQ[searchString]]] *)

enter image description here

Or create a dataset with named columns:

ds2 = Dataset[
 AssociationThread[{"cola", "colb", "colc", "cold", "cole"}, #] & /@ lists] 

enter image description here

ds2[Select[StringContainsQ[searchString]@#cola &]] 

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.