0

I need to know if it is possible with Pandas or Numpy (Python), the comparison of 2 arrays, to identify new items, deleted items, or items with modified prices.

old_list [[tomato, 10.5],[lettuce, 9],[onion, 8],[cucumber, 5]]

new_list [[tomato, 7],[onion, 8],[cucumber, 5],[potato,9]]

Differences between new_list and old_list

Tomato price changed from 10.5 to 7, lettuce was removed and a new product "potato" was added.

1 Answers1

1

Do you need to use arrays. If not you could use dictionaries having the item as the key and the number as the value. then you could check differences under each key and compare list of keys

old = {"tomato": 10.5, "lettuce": 9,"onion": 8, "cucumber": 5}
new = {"tomato": 7,"onion": 8, "cucumber": 5, "potato": 9}

#check if value removed
for key in old.keys():
    if key not in new.keys():
        print(key, "was removed")
# check if value added
for key in new.keys():
    if key not in old.keys():
        print(key, "was added")
# check if a value has changed
for key in old.keys():
    if key in new.keys():
        if old[key] != new[key]:
            print(f"{key} price changed from {old[key]} to {new[key]}")

if you want to use a panda dataframe this stackoverflow post may have your awnser

Taonga07
  • 36
  • 4