1
$\begingroup$

I routinely apply a function to a row and have even learned how to conditionally apply functions to rows in a Mathematica Dataset. However, it feels like I've been staring at the Dataset documentation all day and sadly haven't figured out how to apply a function to multiple columns by row. With Python Pandas I would do something like:

df.apply(lambda row: func(row["columnA"], row["columnB"])

But I'm really struggling with a dataset in the form below. I feel like I'm close but can't figure out the final step.

Dataset in the form of:

a = 
{<|"Name" -> "CLT 3-PLY 4 1/8\" - 3\" CONC TOPPING", "VolumeCF" -> 11221.8749995731, "SupData" -> "18900 SF"|>, 
  <|"Name" -> "CLT 3-PLY 4 1/8\" - 3\" CONC TOPPING", "VolumeCF" -> 2354.06616035646, "SupData" -> "3965 SF"|>, 
  <|"Name" -> "CLT 7-PLY 9 5/8\"- 3\" CONC TOPPING", "VolumeCF" -> 9313.92708286417, "SupData" -> "9234 SF"|>, 
  <|"Name" -> "CLT 7-PLY 9 5/8\"- 3\" CONC TOPPING", "VolumeCF" -> 4071.56249941261, "SupData" -> "3870 SF"|>, 
  <|"Name" -> "CLT 7-PLY 9 5/8\"- 3\" CONC TOPPING", "VolumeCF" -> 9715.46354118723, "SupData" -> "9234 SF"|>, 
  <|"Name" -> "CLT 3-PLY 4 1/8\" - 3\" CONC TOPPING", "VolumeCF" -> 9013.12499949434, "SupData" -> "15180 SF"|>}


stringproc[string_] := Module[{var, a, cltvar, concvar},
  var = StringSplit[string, "-"];
  a = ToExpression /@ StringSplit[
     StringTake[var[[2]], {4, 9}],
     Whitespace
     ];
  cltvar = a[[1]] + N[a[[2]]];
  concvar = StringTake[var[[3]], 2];
  {a , ToExpression@concvar}
  ]

volumeCalculations := Module[{var, clt, conc},
  var = ToExpression@StringSplit[#SupData &, Whitespace][[1]];
  clt = var * (#Name[[1]]/12) & // N;
  conc = var * (#Name[[2]]/12) & // N;
  {clt, conc}
  ]
crunchDataSet[dataframe_] := Module[{var, output},
   var = MapAt[stringproc, dataframe, {All, 1}];
   output = var[All, volumeCalculations];
   output
   ];

crunchDataSet[a[[1]]]

Expected output from the volumeCalculations is

{77962.5, 56700}

Instead I get an error reading below and I can't figure out a permutation that overcomes it.

StringSplit::strse: String or list of strings expected at position 1 in StringSplit[#SupData&,Whitespace].

enter image description here

$\endgroup$
  • 1
    $\begingroup$ What is the output expected to be like? $\endgroup$ – J. M.'s discontentment May 2 at 4:38
  • $\begingroup$ Can you give us a minimum toy example instead? The complication in your code may be obfuscating the problem. $\endgroup$ – MarcoB May 2 at 5:04
  • $\begingroup$ How did you get {77962.5, 56700}? What would the manual calculation of this look like from the given data? $\endgroup$ – J. M.'s discontentment May 2 at 5:30
  • $\begingroup$ It's the first row. The arithmetic is in the volume calculation for clt and conc. {18900 *4.125, 18900*3} $\endgroup$ – BBirdsell May 2 at 5:42
3
$\begingroup$

You may use StringCases and FromDigits inside of Query.

With a as in OP then

Query[All,
  Module[
    {vals = StringCases[#Name, n : NumberString :> FromDigits@n]},
    vals = N@{# + #2/#3 & @@ vals[[2 ;; 4]], Last@vals};
    vals  FromDigits@StringDrop[#SupData, -3]
    ] &
  ]@a
{{77962.5, 56700.}, {16355.6, 11895.}, {88877.3, 27702.}, {37248.8, 11610.}, 
 {88877.3, 27702.}, {62617.5, 45540.}}

Hope this helps.

| improve this answer | |
$\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.