Rename a key in an association?

Posted 8 months ago
1176 Views
|
7 Replies
|
6 Total Likes
|
 I meet this problem When cleaning the raw data every time. How can I agile to rename the key in the association? assoc = Flatten[#] & /@ Transpose[{Range[10], Transpose[{Table[{2019, 10}, {i, 1, 10}], RandomChoice[Range[31], 10], Table[{0, 0, 0}, {i, 1, 10}]}], RandomSample[Range[100], 10]}]; assoc = AssociationThread[{"id", "y", "m", "d", "h", "m", "s", "Value"} -> #] & /@ assoc; assoc[[All, "y"]] = DateString@DateList[#] & /@ Values@Query[All, {"y", "m", "d", "h", "m", "s"}]@assoc; assoc = KeyDrop[assoc, {"m", "d", "h", "m", "s"}] I want to be able to rename Key ["y"] to Key ["date"]. How to do it?
7 Replies
Sort By:
Posted 8 months ago
 You can do this: Association /@ (Normal[assoc] /. "y" -> "Date") (Change it to a list, replace the key, Change it back to Association)Regards.Neil
Posted 8 months ago
 Thanks to Neil and Kuba for their help. All methods can satisfy my needs. After testing, although Kuba's method has the shortest execution time, it will change the order of the fields. So I decided to use the second method provided by Neil! Thanks again for the help of Neil and Kuba ~~ n = 10; assoc = Flatten[#] & /@ Transpose[{Range[n], Transpose[{Table[{2019, 10}, {i, 1, n}], RandomChoice[Range[31], n], Table[{0, 0, 0}, {i, 1, n}]}], RandomChoice[Range[100], n]}]; assoc = AssociationThread[{"id", "y", "m", "d", "h", "m", "s", "Value"} -> #] & /@ assoc; assoc[[All, "y"]] = DateString@DateList[#] & /@ Values@Query[All, {"y", "m", "d", "h", "m", "s"}]@assoc; assoc = KeyDrop[assoc, {"m", "d", "h", "m", "s"}]; (*The second method provided by Neil*) assoc = Map[KeyMap[ReplaceAll["y" -> "Date"], #] &, assoc]; 
Posted 8 months ago
 or this: Map[KeyMap[ReplaceAll["y" -> "Date"], #] &, assoc] Keep the association and KeyMap the replacement.Regards.
Posted 8 months ago
 Another alternative: <|"Date" -> #y, KeyDrop["y"]@#|> & /@ assoc 
Posted 8 months ago
 KeyMap can be used directly In[1]:= assoc = <|"a" -> 7, "b" -> 8, "c" -> 9|> Out[1]= <|"a" -> 7, "b" -> 8, "c" -> 9|> In[2]:= KeyMap[# /. "b" -> "qq" &, assoc] Out[2]= <|"a" -> 7, "qq" -> 8, "c" -> 9|> 
 Sir. I don't know why the following error message appears during execution!  n = 10; assoc = Flatten[#] & /@ Transpose[{Range[n], Transpose[{Table[{2019, 10}, {i, 1, n}], RandomChoice[Range[31], n], Table[{0, 0, 0}, {i, 1, n}]}], RandomChoice[Range[100], n]}]; assoc = AssociationThread[{"id", "y", "m", "d", "h", "m", "s", "Value"} -> #] & /@ assoc; assoc[[All, "y"]] = DateString@DateList[#] & /@ Values@Query[All, {"y", "m", "d", "h", "m", "s"}]@assoc; assoc = KeyDrop[assoc, {"m", "d", "h", "m", "s"}]; (*Provides by Hans Milton*) assoc = KeyMap[# /. "y" -> "date" &, assoc] // TableForm KeyMap::invak: The argument {<|id->1,y->Fri 31 Jul 2020 00:00:00,Value->81|>,<|id->2,y->Fri 30 Apr 2021 00:00:00,Value->43|>,<|id->3,y->Mon 31 Dec 2018 00:00:00,Value->11|>,<|id->4,y->Wed 31 Mar 2021 00:00:00,Value->79|>,<|id->5,y->Tue 30 Apr 2019 00:00:00,Value->1|>,<|id->6,y->Sun 31 Mar 2019 00:00:00,Value->44|>,<|id->7,y->Fri 31 Jul 2020 00:00:00,Value->11|>,<|id->8,y->Mon 31 May 2021 00:00:00,Value->27|>,<|id->9,y->Sat 31 Oct 2020 00:00:00,Value->38|>,<|id->10,y->Thu 30 Apr 2020 00:00:00,Value->9|>} is not a valid Association.