Is there a way to return multiple values from csv file in function with statistics? (string & float)

129 Views Asked by At

I'm pretty new to this - I'm working on a basic sales csv file to extract multiple values.. The csv contains a list of months and the number of sales for that month as well as other columns but these are the only two I'm working with below.

I was wondering how I might be able to return two corresponding fields?

Below is some working code where I am getting the highest sales figure for a month - I also want to return the month name itself next to the figure.. I've tried a few things but I haven't managed to get it to work.

Any help and advice would be appreciated!! :)

header = ["year", "month", "sales", "expenditure"]

def run():
    data = read_data()
    sales = []

    for row in data:
        min_sale = int(row["sales"])
        sales.append(min_sale)

    minny = min(sales)
    print("Minimum sales is month: {}".format(minny))

run()
1

There are 1 best solutions below

0
Zach Young On

You can continue down the path of appending to a list then picking the smallest. Instead of appending to a list of only integers (sales numbers), you'll append a tuple of the sales number and the month:

sales.append((min_sale, row["month"]))

When you call min(sales), the selection function will first compare the sales integer for any pair of tuples. If there's a smaller int, that one "wins". If both ints are equal, the comparison will move on to compare the month strings:

min([(9, "Jun"), (10, "Apr")])  # returns (9, 'Jun')

min([(9, "Jun"), ( 9, "Apr")])  # returns (9, 'Apr')

Here's a complete sample:

data = [
    {"year": "2020", "month": "Jan", "sales": "345", "expenditure": "23"},
    {"year": "2020", "month": "Feb", "sales": "67", "expenditure": "0"},
    {"year": "2020", "month": "Mar", "sales": "2000", "expenditure": "100"},
    {"year": "2020", "month": "Apr", "sales": "9", "expenditure": "87"},
    {"year": "2020", "month": "May", "sales": "459", "expenditure": "230"},
]


sales = []
for row in data:
    min_sale = int(row["sales"])
    sales.append((min_sale, row["month"]))

minny = min(sales)
print(f"Minimum sale was {minny[0]} in month {minny[1]}")

That prints:

Minimum sale was 9 in month Apr

The next step might be to just call min() on data, and provide a key= param that tells min specifically what to consider for finding the minimum:

def sales_int(row):
    return int(row["sales"])


minny = min(data, key=sales_int)
print(f"Minimum sale was {minny['sales']} in month {minny['month']}")

That prints the same as above.

min passes each element of data (a "dict" row) to sales_int, which returns the int-ified sales number, that number is then used in the comparison to find the minimum. You can try making another func, like expenditure_int which returns an int-ified expenditure.

If you wanted to break a tie by month name, like above:

def sales_month(row):
    return int(row["sales"]), row["month"]

You can use the same concept of passing a key function when sorting a list, too: data.sort(key=sale_int).