diff --git a/lab-web-scraping.ipynb b/lab-web-scraping.ipynb index e552783..bb5a805 100644 --- a/lab-web-scraping.ipynb +++ b/lab-web-scraping.ipynb @@ -110,15 +110,538 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "40359eee-9cd7-4884-bfa4-83344c222305", "metadata": { "id": "40359eee-9cd7-4884-bfa4-83344c222305" }, "outputs": [], "source": [ - "# Your solution goes here" + "import pandas as pd\n", + "import requests\n", + "from bs4 import BeautifulSoup" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6c4d24a5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "url = \"https://books.toscrape.com/\"\n", + "response = requests.get(url)\n", + "response" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9bd160bb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "b'\\n\\n\\n\\n \\n \\n \\n All products | Books to Scrape - Sandbox\\n\\n\\n \\n \\n \\n \\n \\n\\n \\n \\n\\n \\n \\n \\n\\n \\n \\n \\n \\n \\n \\n \\n \\n\\n\\n \\n \\n\\n \\n\\n \\n \\n \\n\\n \\n \\n\\n \\n \\n \\n \\n \\n
\\n
\\n
\\n
Books to Scrape We love being scraped!\\n
\\n\\n \\n
\\n
\\n
\\n\\n \\n \\n
\\n
\\n \\n
    \\n
  • \\n Home\\n
  • \\n
  • All products
  • \\n
\\n\\n
\\n\\n \\n\\n
\\n \\n
\\n

All products

\\n
\\n \\n\\n \\n\\n\\n\\n
\\n\\n
\\n\\n\\n
\\n \\n
\\n\\n \\n
\\n \\n
\\n \\n \\n
\\n\\n \\n \\n \\n 1000 results - showing 1 to 20.\\n \\n \\n \\n \\n
\\n \\n
\\n
Warning! This is a demo website for web scraping purposes. Prices and ratings here were randomly assigned and have no real meaning.
\\n\\n
\\n
    \\n \\n
  1. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"A\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    A Light in the ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa351.77

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  2. \\n \\n
  3. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Tipping\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Tipping the Velvet

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa353.74

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  4. \\n \\n
  5. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Soumission\"\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Soumission

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa350.10

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  6. \\n \\n
  7. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Sharp\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Sharp Objects

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa347.82

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  8. \\n \\n
  9. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Sapiens:\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Sapiens: A Brief History ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa354.23

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  10. \\n \\n
  11. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"The\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    The Requiem Red

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa322.65

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  12. \\n \\n
  13. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"The\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    The Dirty Little Secrets ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa333.34

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  14. \\n \\n
  15. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"The\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    The Coming Woman: A ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa317.93

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  16. \\n \\n
  17. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"The\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    The Boys in the ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa322.60

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  18. \\n \\n
  19. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"The\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    The Black Maria

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa352.15

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  20. \\n \\n
  21. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Starving\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Starving Hearts (Triangular Trade ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa313.99

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  22. \\n \\n
  23. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Shakespeare's\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Shakespeare's Sonnets

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa320.66

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  24. \\n \\n
  25. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Set\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Set Me Free

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa317.46

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  26. \\n \\n
  27. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Scott\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Scott Pilgrim's Precious Little ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa352.29

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  28. \\n \\n
  29. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Rip\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Rip it Up and ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa335.02

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  30. \\n \\n
  31. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Our\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Our Band Could Be ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa357.25

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  32. \\n \\n
  33. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Olio\"\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Olio

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa323.88

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  34. \\n \\n
  35. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Mesaerion:\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Mesaerion: The Best Science ...

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa337.59

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  36. \\n \\n
  37. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"Libertarianism\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    Libertarianism for Beginners

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa351.33

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  38. \\n \\n
  39. \\n\\n\\n\\n\\n\\n\\n
    \\n \\n
    \\n \\n \\n \"It's\\n \\n \\n
    \\n \\n\\n \\n \\n

    \\n \\n \\n \\n \\n \\n

    \\n \\n \\n\\n \\n

    It's Only the Himalayas

    \\n \\n\\n \\n
    \\n \\n\\n\\n\\n\\n\\n\\n \\n

    \\xc2\\xa345.17

    \\n \\n\\n

    \\n \\n \\n In stock\\n \\n

    \\n\\n \\n \\n\\n\\n\\n\\n\\n\\n \\n
    \\n \\n
    \\n\\n\\n \\n
    \\n \\n
    \\n\\n
  40. \\n \\n
\\n \\n\\n\\n\\n
\\n
    \\n \\n
  • \\n \\n Page 1 of 50\\n \\n
  • \\n \\n
  • next
  • \\n \\n
\\n
\\n\\n\\n
\\n
\\n \\n\\n\\n
\\n\\n
\\n
\\n
\\n\\n\\n \\n\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n\\n \\n \\n\\n\\n \\n \\n \\n\\n \\n\\n\\n \\n \\n\\n \\n \\n \\n \\n\\n'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "response.content" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "cabe8cc3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Date': 'Sat, 18 Oct 2025 13:52:37 GMT', 'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Last-Modified': 'Wed, 08 Feb 2023 21:02:32 GMT', 'ETag': 'W/\"63e40de8-c85e\"', 'Strict-Transport-Security': 'max-age=0; includeSubDomains; preload', 'Content-Encoding': 'br'}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "response.headers" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "52b262a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "text/html\n" + ] + } + ], + "source": [ + "print(response.headers[\"content-type\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "82bd26d3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "bs4.BeautifulSoup" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "soup = BeautifulSoup(response.content, \"html.parser\")\n", + "type(soup)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "44b63631", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20\n" + ] + } + ], + "source": [ + "books = soup.find_all(\"article\", class_=\"product_pod\") \n", + "print(len(books))" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "1609e5dd", + "metadata": {}, + "outputs": [], + "source": [ + "book = books[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "8735b4be", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A Light in the Attic\n" + ] + } + ], + "source": [ + "title = book.h3.a[\"title\"]\n", + "print(title)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6eef86dc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "£51.77\n" + ] + } + ], + "source": [ + "price = book.find(\"p\", class_=\"price_color\").text\n", + "print(price)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "d93356bb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Three\n" + ] + } + ], + "source": [ + "rating_c = book.p[\"class\"]\n", + "rating = rating_c[1]\n", + "print(rating)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "00aa7a9f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "In stock\n" + ] + } + ], + "source": [ + "availability = book.find(\"p\", class_=\"instock availability\").text.strip()\n", + "print(availability)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d7c404e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A Light in the Attic £51.77 Three In stock\n", + "------------\n", + "Tipping the Velvet £53.74 One In stock\n", + "------------\n", + "Soumission £50.10 One In stock\n", + "------------\n", + "Sharp Objects £47.82 Four In stock\n", + "------------\n", + "Sapiens: A Brief History of Humankind £54.23 Five In stock\n", + "------------\n", + "The Requiem Red £22.65 One In stock\n", + "------------\n", + "The Dirty Little Secrets of Getting Your Dream Job £33.34 Four In stock\n", + "------------\n", + "The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull £17.93 Three In stock\n", + "------------\n", + "The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics £22.60 Four In stock\n", + "------------\n", + "The Black Maria £52.15 One In stock\n", + "------------\n", + "Starving Hearts (Triangular Trade Trilogy, #1) £13.99 Two In stock\n", + "------------\n", + "Shakespeare's Sonnets £20.66 Four In stock\n", + "------------\n", + "Set Me Free £17.46 Five In stock\n", + "------------\n", + "Scott Pilgrim's Precious Little Life (Scott Pilgrim #1) £52.29 Five In stock\n", + "------------\n", + "Rip it Up and Start Again £35.02 Five In stock\n", + "------------\n", + "Our Band Could Be Your Life: Scenes from the American Indie Underground, 1981-1991 £57.25 Three In stock\n", + "------------\n", + "Olio £23.88 One In stock\n", + "------------\n", + "Mesaerion: The Best Science Fiction Stories 1800-1849 £37.59 One In stock\n", + "------------\n", + "Libertarianism for Beginners £51.33 Two In stock\n", + "------------\n", + "It's Only the Himalayas £45.17 Two In stock\n", + "------------\n" + ] + } + ], + "source": [ + "\n", + "for book in books:\n", + " title = book.h3.a[\"title\"]\n", + " price = book.find(\"p\", class_=\"price_color\").text\n", + " rating = book.p[\"class\"][1]\n", + " availability = book.find(\"p\", class_=\"instock availability\").text.strip()\n", + "\n", + " print(title, price, rating, availability)\n", + " print(\"------------\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "0f143c15", + "metadata": {}, + "outputs": [], + "source": [ + "book_list = []\n", + "for book in books:\n", + " title = book.h3.a[\"title\"]\n", + " price = book.find(\"p\", class_=\"price_color\").text\n", + " rating = book.p[\"class\"][1]\n", + " availability = book.find(\"p\", class_=\"instock availability\").text.strip()\n", + "\n", + " book_list.append({\"Title\": title, \"Price\": float(price[2:]), \"Ratings\": rating, \"Availability\": availability})" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "9d622618", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.DataFrame(book_list)\n" ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "0489b03d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
TitlePriceRatingsAvailability
0A Light in the Attic1.77ThreeIn stock
1Tipping the Velvet3.74OneIn stock
2Soumission0.10OneIn stock
3Sharp Objects7.82FourIn stock
4Sapiens: A Brief History of Humankind4.23FiveIn stock
\n", + "
" + ], + "text/plain": [ + " Title Price Ratings Availability\n", + "0 A Light in the Attic 1.77 Three In stock\n", + "1 Tipping the Velvet 3.74 One In stock\n", + "2 Soumission 0.10 One In stock\n", + "3 Sharp Objects 7.82 Four In stock\n", + "4 Sapiens: A Brief History of Humankind 4.23 Five In stock" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "1ceb43c3", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/var/folders/lt/yxd_bdjs4fn9bd90z539wlsm0000gn/T/ipykernel_5784/2712251561.py:1: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n", + " df[\"Ratings\"] = df[\"Ratings\"].replace({\"One\" : 1, \"Two\" : 2, \"Three\" : 3, \"Four\" : 4, \"Five\" : 5})\n" + ] + } + ], + "source": [ + "df[\"Ratings\"] = df[\"Ratings\"].replace({\"One\" : 1, \"Two\" : 2, \"Three\" : 3, \"Four\" : 4, \"Five\" : 5})\n" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "691668f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
TitlePriceRatingsAvailability
3Sharp Objects7.824In stock
4Sapiens: A Brief History of Humankind4.235In stock
6The Dirty Little Secrets of Getting Your Dream...3.344In stock
8The Boys in the Boat: Nine Americans and Their...2.604In stock
11Shakespeare's Sonnets0.664In stock
\n", + "
" + ], + "text/plain": [ + " Title Price Ratings \\\n", + "3 Sharp Objects 7.82 4 \n", + "4 Sapiens: A Brief History of Humankind 4.23 5 \n", + "6 The Dirty Little Secrets of Getting Your Dream... 3.34 4 \n", + "8 The Boys in the Boat: Nine Americans and Their... 2.60 4 \n", + "11 Shakespeare's Sonnets 0.66 4 \n", + "\n", + " Availability \n", + "3 In stock \n", + "4 In stock \n", + "6 In stock \n", + "8 In stock \n", + "11 In stock " + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "filtered = df[(df[\"Ratings\"] >=4) & (df[\"Price\"] <= 20)]\n", + "filtered.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f417b27", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -126,7 +649,7 @@ "provenance": [] }, "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -140,7 +663,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.2" } }, "nbformat": 4,