1

I want to get the minimum price first in a list... this is my code

for link in productlinks:
    try:
        r = requests.get(link, headers=headers)
        soup = BeautifulSoup(r.content, 'lxml')
        name = soup.find(
            'h1', class_='product-main__name').text.strip()
        price = soup.find(
            'p', class_='product-action__price').text.strip()
        price = price.replace('£', '')
        Aitems = {
            'price': price,
            'name': name
        }
        itemlist.append(Aitems)
        print('Saving:', Aitems['price'])
    except AttributeError:
        continue
df = pd.DataFrame(itemlist)
print(min(df['price']))

Output:

Saving: 30.45
Saving: 31.95
Saving: 32.75
Saving: 32.95
Saving: 29.45
Saving: 38.95
Saving: 40.95
29.45

I can get the minimum value of that code but I want the whole list of the "products" so it starts with the minimum value up to the maximum value.

Output
                                                 name  price
0                               Suntory Torys Classic  30.45 < "I want it to start with the minimum value"
1                                        Suntory Toki  31.95
2                               Akashi Blended Whisky  32.75
3                       Tokinoka White Blended Whisky  32.95
4                    Hatozaki Blended Japanese Whisky  29.45
5                                          Nikka Days  38.95

Is there a simple way I can do it?

I tried

print(df.sort_values(by=['price']))

But it did not sort the minimum price. It was kind of random numbers. Here is the output:

38                    Ichiro's Malt Wine Wood Reserve    115
37           Ichiro's Malt MWR\nMizunara Wood Reserve    115
52                Suntory Yamazaki Puncheon\nBot.2013   1200
51          Suntory Yamazaki Bourbon Barrel\nBot.2013   1200
39                       Suntory Yamazaki 12 Year Old    125
40                                Okayama Single Malt    147
D3lta Nuub
  • 11
  • 3
  • 1
    It sounds like you want to sort the list? – Ryan Schaefer Oct 12 '20 at 17:09
  • [`sorted()`](https://docs.python.org/3/library/functions.html#sorted)? – Tomerikoo Oct 12 '20 at 17:09
  • Does this answer your question? [How do I sort a list of dictionaries by a value of the dictionary?](https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary) – Tomerikoo Oct 12 '20 at 17:13
  • 1
    Welcome to SO! Check out the [tour]. Please provide something like a [mre], with input, minimal code, and desired output. It sounds like you just want to sort a list, no? That should be covered by any Python tutorial. – wjandrea Oct 12 '20 at 17:13
  • Oops, sorry, I should have read the code first. Your description is very misleading. That's not a list, it's a `Pandas.Series`. Do you want to save the result back to the df? – wjandrea Oct 12 '20 at 17:16
  • @RyanSchaefer yeah sort the price, start with the minimum value. – D3lta Nuub Oct 12 '20 at 17:20
  • @Tomerikoo I don't know how to put it. I tried df = pd.DataFrame(itemlist) print(df.sort()) but got an error. – D3lta Nuub Oct 12 '20 at 17:21
  • 1
    This might be a duplicate of [how to sort pandas dataframe from one column](https://stackoverflow.com/q/37787698/4518341) – wjandrea Oct 12 '20 at 17:25

1 Answers1

0

I assume you are using Pandas, as you have a "pd.Dataframe" in your code. A Pandas DataFrame can be sorted by

df = df.sort_values(by='price')

Another more general way (not involving pandas) would to generate a list if the indices your items should to be sorted. With this indexList you sort your itemlist.

How to create the itemlist can be seen here How to get indices of a sorted array in Python.

Creating the inxList with numpy you get this solution for your problem

import numpy as np
inxList = np.argsort([item['price'] for item in itemlist])
itemlist = [itemlist[inx] for inx in inxList]
MMeissner
  • 46
  • 3