Home > Blockchain >  How do i group product data with multiple of the same products per store
How do i group product data with multiple of the same products per store

Time:10-20

I'm building a pyhon price tracker which allows me to enter some urls. The program then fetches their prices once each day and adds it to the csv file below.

I'm now looking for a way to show the price evolution of each product with a line chart but cannot find a way to get something like this below where i have prices from a product from a specific store.

Samsung Galaxy Tab S6 lite - coolblue = [123.00, 156.00, 186.00]

csv dataset

CodePudding user response:

Sounds like you're after a dictionary of store items with a list of prices. To achieve this you could do something along the lines of:

#Import a package to allow automatic creations of dictionaries containing lists
from collection import defaultdict

price_dictionary = defaultdict("list")

#Loop through lines, split on commas, append list items as values to product name keys
for line in data.csv:
    temp_list = []
    temp_list = line.split(",")
    price_dictionary[temp_list[0]].append(temp_list[3])

This isn't written as succinctly as possible but hopefully gives you an idea of you could do for your file.

CodePudding user response:

I think you need itertools.groupby and some string handling like this

text = """Samsung Galaxy Tab So lite, 2022-10-17, coolblue, 501.99 
Samsung Galaxy Tab S7 FE, 2022-10-17, coolblue, 589.00 
Samsung Galaxy Tab S7 FE, 2022-10-17, bol, 596.99 
Raspberry Pi 4 8 GB, 2022-10-17, alternate, 129.90 
Samsung Galaxy Tab S7 FE, 2022-10-17, alternate, 549.00 
Google Nest Mini, 2022-10-17, coolblue, 59.99 
Samsung Galaxy Tab S6 lite, 2022-10-18, coolblue, 501.99 
Samsung Galaxy Tab S7 FE, 2822-10-18, coolblue, 589.0 
Samsung Galaxy Tab S7 FE, 2022-10-18, bol, 596.99 
Raspberry Pi 4 8 GB, 2022-10-18, alternate, 129.9 
Samsung Galaxy Tab S7 FE, 2022-10-18, alternate, 549.0 
Google Nest Mini, 2022-10-18, coolblue, 59.99 
Samsung Galaxy Tab S6 lite, 2022-18-19, coolblue, 501.99 
Samsung Galaxy Tab S7 FE, 2822-10-19, coolblue, 589.0 
Samsung Galaxy Tab S7 FE, 2022-10-19, bol, 596.99 
Raspberry Pi 4 8 GB, 2822-18-19, alternate, 129.9 
Samsung Galaxy Tab S7 FE, 2022-10-19, alternate, 549.0 
Google Nest Mini, 2022-10-19, coolblue, 59.99
"""
from itertools import groupby
for k, v in groupby(sorted(text.splitlines(), key=lambda x: x.split(",")[0]), key=lambda x: (x.split(",")[0], x.split(",")[2])):
    print(k, [val.split(",")[-1] for val in v])

I have used google lens to extract text from image so it may be incorrect

Output

('Google Nest Mini', ' coolblue') [' 59.99 ', ' 59.99 ', ' 59.99']
('Raspberry Pi 4 8 GB', ' alternate') [' 129.90 ', ' 129.9 ', ' 129.9 ']
('Samsung Galaxy Tab S6 lite', ' coolblue') [' 501.99 ', ' 501.99 ']
('Samsung Galaxy Tab S7 FE', ' coolblue') [' 589.00 ']
('Samsung Galaxy Tab S7 FE', ' bol') [' 596.99 ']
('Samsung Galaxy Tab S7 FE', ' alternate') [' 549.00 ']
('Samsung Galaxy Tab S7 FE', ' coolblue') [' 589.0 ']
('Samsung Galaxy Tab S7 FE', ' bol') [' 596.99 ']
('Samsung Galaxy Tab S7 FE', ' alternate') [' 549.0 ']
('Samsung Galaxy Tab S7 FE', ' coolblue') [' 589.0 ']
('Samsung Galaxy Tab S7 FE', ' bol') [' 596.99 ']
('Samsung Galaxy Tab S7 FE', ' alternate') [' 549.0 ']
('Samsung Galaxy Tab So lite', ' coolblue') [' 501.99 ']

For File base

from itertools import groupby

with open("prices.csv") as f:
    text = f.read().splitlines()[1:]
    for k, v in groupby(sorted(text, key=lambda x: x.split(",")[1]), key=lambda x: (x.split(",")[1], x.split(",")[3])):
        print(k, [val.split(",")[-1] for val in v])
        print(" - ".join(list(k)), " = ", [val.split(",")[-1] for val in v]) # if you want the output like you have given in the question
  • Related