Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.6K Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Update prices of two catalogues

Posted 3 years ago

I have the following 2 datasets, each describing a catalogue of items: the item's part number, price and verbal description. The left dataset is the out-of-date catalogue and the right one is the up-to-date catalogue.

enter image description here

Note that it is possible that there are items in the new catalogue that are not present in the old one, as happens with "spanner".

Question: How can I create a new data set with the prices updated?

Data is

old = Dataset[{
    <|"part nu" -> "045", "price" -> 8.9, "description" -> "cutter"|>,
    <|"part nu" -> "014", "price" -> 9.3, "description" -> "plier"|>,
    <|"part nu" -> "106", "price" -> 4.6, "description" -> "pipe wrench"|>,
    <|"part nu" -> "183", "price" -> 24.1, "description" -> "screwdriver"|>,
    <|"part nu" -> "283", "price" -> 0.8, "description" -> "washer"|>
   }]

new = Dataset[{
    <|"part nu" -> "045", "price" -> 9.2, "description" -> "cutter"|>,
    <|"part nu" -> "014", "price" -> 9.5, "description" -> "plier"|>,
    <|"part nu" -> "612", "price" -> 10.9, "description" -> "spanner"|>,
    <|"part nu" -> "106", "price" -> 4.2, "description" -> "pipe wrench"|>,
    <|"part nu" -> "183", "price" -> 24., "description" -> "screwdriver"|>,
    <|"part nu" -> "283", "price" -> 0.8, "description" -> "washer"|>
  }]
POSTED BY: Ehud Behar
5 Replies
UpdateOldDataset[old_, new_, key_] := Join[
  Complement[old, new, SameTest -> (#1[[key]] == #2[[key]] &)],
  Intersection[new, old, SameTest -> (#1[[key]] == #2[[key]] &)]]

Robert

POSTED BY: Robert Nowak

Hi Ehud,

I think your data makes no sense, because your new catalogue is a superset of the old one. As such the new catalogue contains up to date new prices and all new and old parts are included.

It would make more sense if the old catalogue would contain items not present in the new one.

UpdateDataset[old_, new_, key_] := 
 Join[Complement[old, new, SameTest -> (#1[[key]] == #2[[key]] &)], new]

UpdateDataset[old, new, "description"]
UpdateDataset[new, old, "description"]

you decide which way around to go ...

Robert

POSTED BY: Robert Nowak
Posted 3 years ago

Thanks a lot! You are right, the new one is a superset of the old one, and my question indeed makes no sense, but only at the point of view of this MWE. I have some other problems to deal with when working with the real catalogues.

The expression UpdateDataset[old, new, "description"] seems to be the one I should work with. But, it adds "spanner" to the new generated data set. Since "spanner" is not present in the old list, I don't want it to appear in the newly generated list.

Is there a way to not add those items that not appear in the "new" catalogue?

POSTED BY: Ehud Behar

Do you want the old catalogue but with the new prices?

POSTED BY: Robert Nowak
Posted 3 years ago

Exactly

POSTED BY: Ehud Behar
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard