0
$\begingroup$

I have asked a couple questions here (and received terrific answers), and used that info.

I am trying to fulfill a request from a colleague that liked the results of Astronomy info I had done, but wanted it in a flat, 2D format (along with sun and mood data, but one task at a time.

I manged the grid ok enough (attached), but once again I cannot change the units associated with these quantities from g/cm^3 to kg/m^3. I have tried what others helped with in my previous questions, but, for whatever reason I'm not getting it....and if it's me, well, that's possible, but I am trying.

Can the units be changed, and if so how, because I am (once again) out of ideas. I have been looking at this in the documentation and online for a few days now and it's escaping me.

Thanks (again) for your help.

enter image description here

$\endgroup$
1
  • 4
    $\begingroup$ Post code, not pictures of code. Post the output as an image, but the input as copyable. It is failing because UnitConvert should take a Quantity as the input, not a grid. You either need to Map the unit conversion to the appropriate level or use a replacement rule like Text[<your code here>] /. q_Quantity :> UnitConvert[q, "Stones"/"Gallons"] $\endgroup$
    – Jason B.
    Commented Jan 22, 2021 at 19:24

2 Answers 2

6
$\begingroup$
data = PlanetData[PlanetData[], {"Name", "Density"}] /. {p_, m_} :> {p, UnitConvert[m, "SIBase"]} // 
         Prepend[{"Name", "Density"}]

Grid[data, Frame -> All, Background -> LightYellow, ItemStyle -> {Automatic, 1 -> Bold}]

enter image description here

$\endgroup$
4
  • $\begingroup$ Exactly what I needed....I'm going to have to try and understand what you did here, because I was completely stuck, and I thank you for the help. $\endgroup$
    – fiz
    Commented Jan 23, 2021 at 1:55
  • $\begingroup$ Rohit....I guess I didn't recognize your name. You help a LOT and I appreciate it. I try to understand what is provided, but sometimes it's not always clear. Nevertheless thank you for helping! $\endgroup$
    – fiz
    Commented Jan 27, 2021 at 19:11
  • 1
    $\begingroup$ @fiz You are welcome. My answer and @Bob Hanlon's answer both use/. to do the unit conversion. /. is short for ReplaceAll. $\endgroup$ Commented Jan 28, 2021 at 2:31
  • $\begingroup$ Ok....thank you once again. I'm trying to learn this (and also Python as well)....ugh....but you're help is appreciated, and I do read it and take it to heart. Thanks Rohit! $\endgroup$
    – fiz
    Commented Feb 1, 2021 at 18:33
3
$\begingroup$

With a little more color:

Clear["Global`*"]

items = {"Image", "Name", "Density"};

(data = PlanetData[PlanetData[], items] /. 
     q_Quantity :> UnitConvert[q] // Prepend[items]) //
 Grid[#, Frame -> All, Background -> LightYellow, 
   ItemStyle -> {Automatic, 1 -> Bold}] &

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you, this is BEAUTIFUL, and beautifully done. I only chose the other answer because it was all I needed and it arrived first, but I took this code and WILL utilize it, and appreciate it very much. I wish I knew this code as well as you both do....practice I guess. $\endgroup$
    – fiz
    Commented Jan 23, 2021 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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