Skip to content

Instantly share code, notes, and snippets.

@pybokeh
Last active December 27, 2015 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pybokeh/7294874 to your computer and use it in GitHub Desktop.
Save pybokeh/7294874 to your computer and use it in GitHub Desktop.
How to Parse TF Backpack Inventory Items
{
"metadata": {
"name": "TF2 Backpack Inventory Parser"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "This is an IPython notebook that demonstrates how to get information on items in a person's Team Fortress 2 backpack with a link/URL that they provided you normally via email. We will be using the BeautifulSoup package that will help us parse the HTML page and grab the data we want that is embedded in the HTML markup code.<br><br>\nThis IPython notebook example assumes that we will be using this URL: http://www.tf2wh.com/backpack?bp=J4YdIfPh-kGWIgIYA39izmnTWlRcXrpGUOHejlqu2E4<br>\nThis is an actual, working URL belonging to my son's TF2 backpack."
},
{
"cell_type": "code",
"collapsed": false,
"input": "from bs4 import BeautifulSoup # Not part of Python language, a 3rd-party package for parsing HTML pages\nimport urllib.request as request # Part of Python language used to handle URLs",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 50
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Here, we are going to prompt the user to copy and then paste the TF2 backpack inventory URL into the textbox:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "#html = request.urlopen('http://www.tf2wh.com/backpack?bp=J4YdIfPh-kGWIgIYA39izmnTWlRcXrpGUOHejlqu2E4')\nhtml = request.urlopen(input(\"Paste the TF2 WH Inventory URL here: \"))\nsoup = BeautifulSoup(html)",
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Paste the TF2 WH Inventory URL here: http://www.tf2wh.com/backpack?bp=J4YdIfPh-kGWIgIYA39izmnTWlRcXrpGUOHejlqu2E4\n"
}
],
"prompt_number": 51
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Then, we need to look at the source-code of that HTML page. We can open the page using the browser, but I will open a new tab with it:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "import webbrowser\nwebbrowser.open_new_tab('http://www.tf2wh.com/backpack?bp=J4YdIfPh-kGWIgIYA39izmnTWlRcXrpGUOHejlqu2E4')",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 52,
"text": "True"
}
],
"prompt_number": 52
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "After examining that HTML page's source code, we can see that the information that we need is contained within the table body tag \"tbody\". Now let's see what it looks like:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "table_body = soup.tbody\ntable_body",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 54,
"text": "<tbody>\n<tr class=\"overstocked\"><th><font color=\"#4D7455\">Genuine Companion Cube Pin</font></th><td>2,925c</td><td>1</td><td>15</td><td></td><td>15\n</td></tr><tr class=\"overstocked\"><th><font color=\"#7D6D00\">El Jefe</font></th><td>11,040c</td><td>1</td><td>30</td><td></td><td>30\n</td></tr><tr class=\"overstocked\"><th><font color=\"#7D6D00\">Fall 2013 Acorns Crate</font></th><td>310c</td><td>2</td><td>12</td><td></td><td>10\n</td></tr><tr class=\"overstocked\"><th><font color=\"#7D6D00\">Fall 2013 Gourd Crate</font></th><td>310c</td><td>4</td><td>13</td><td></td><td>10\n</td></tr><tr class=\"overstocked\"><th><font color=\"#7D6D00\">Point and Shoot</font></th><td>6,185c</td><td>1</td><td>25</td><td></td><td>25\n</td></tr><tr class=\"overstocked\"><th><font color=\"#7D6D00\">Strange Part: Gib Kills</font></th><td>5,460c</td><td>1</td><td>5</td><td></td><td>5\n</td></tr><tr class=\"overstocked\"><th><font color=\"#CF6A32\">Strange Enforcer</font></th><td>1,600c</td><td>1</td><td>16</td><td></td><td>15\n</td></tr><tr class=\"overstocked\"><th><font color=\"#CF6A32\">Strange Medi Gun</font></th><td>2,700c</td><td>1</td><td>40</td><td></td><td>40\n</td></tr><tr class=\"overstocked\"><th><font color=\"#CF6A32\">Strange Neon Annihilator</font></th><td>370c</td><td>1</td><td>27</td><td></td><td>25\n</td></tr><tr class=\"overstocked\"><th><font color=\"#8650AD\">Haunted B-ankh!</font></th><td>3,420c</td><td>1</td><td>3</td><td></td><td>3\n</td></tr></tbody>"
}
],
"prompt_number": 54
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "We have the table body parsed, but we really need the table rows parsed, since that is where the table row data is located at."
},
{
"cell_type": "code",
"collapsed": false,
"input": "# Within table_body tag, find all table row \"tr\" tags\ntable_rows = table_body.find_all('tr')\ntable_rows",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 55,
"text": "[<tr class=\"overstocked\"><th><font color=\"#4D7455\">Genuine Companion Cube Pin</font></th><td>2,925c</td><td>1</td><td>15</td><td></td><td>15\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#7D6D00\">El Jefe</font></th><td>11,040c</td><td>1</td><td>30</td><td></td><td>30\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#7D6D00\">Fall 2013 Acorns Crate</font></th><td>310c</td><td>2</td><td>12</td><td></td><td>10\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#7D6D00\">Fall 2013 Gourd Crate</font></th><td>310c</td><td>4</td><td>13</td><td></td><td>10\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#7D6D00\">Point and Shoot</font></th><td>6,185c</td><td>1</td><td>25</td><td></td><td>25\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#7D6D00\">Strange Part: Gib Kills</font></th><td>5,460c</td><td>1</td><td>5</td><td></td><td>5\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#CF6A32\">Strange Enforcer</font></th><td>1,600c</td><td>1</td><td>16</td><td></td><td>15\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#CF6A32\">Strange Medi Gun</font></th><td>2,700c</td><td>1</td><td>40</td><td></td><td>40\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#CF6A32\">Strange Neon Annihilator</font></th><td>370c</td><td>1</td><td>27</td><td></td><td>25\n</td></tr>,\n <tr class=\"overstocked\"><th><font color=\"#8650AD\">Haunted B-ankh!</font></th><td>3,420c</td><td>1</td><td>3</td><td></td><td>3\n</td></tr>]"
}
],
"prompt_number": 55
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Now that we have the table rows stored in the table_rows variable, we can then extract the individual information we want from it:"
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "For example, let's extract each item's inventory status:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "# For each table row, print the status (or actually the \"class\" value attribute)\nfor row in table_rows:\n print(row['class'])",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "['overstocked']\n['overstocked']\n['overstocked']\n['overstocked']\n['overstocked']\n['overstocked']\n['overstocked']\n['overstocked']\n['overstocked']\n['overstocked']\n"
}
],
"prompt_number": 56
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Extracting each item's name:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "for row in table_rows:\n print(row.font.get_text())",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Genuine Companion Cube Pin\nEl Jefe\nFall 2013 Acorns Crate\nFall 2013 Gourd Crate\nPoint and Shoot\nStrange Part: Gib Kills\nStrange Enforcer\nStrange Medi Gun\nStrange Neon Annihilator\nHaunted B-ankh!\n"
}
],
"prompt_number": 57
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "We can extract each item's price...but wait a minute. The price is in the \"td\" tag, but there are other \"td\" tags too. How do we tell or how does BeautifulSoup knows which \"td\" tag to pull the data from?<br><br>\nBut let's just try this and see what happens:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "for row in table_rows:\n print(row.td.get_text())",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "2,925c\n11,040c\n310c\n310c\n6,185c\n5,460c\n1,600c\n2,700c\n370c\n3,420c\n"
}
],
"prompt_number": 58
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "OK so looks like it grabbed the data in the \"Price\" column, but how do we get the data in the \"You\" column and other columns as well?"
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "I tried this thinking maybe to get the other columns, I just need to reference the proper index maybe??"
},
{
"cell_type": "code",
"collapsed": false,
"input": "for row in table_rows:\n print(row.td[1].get_text())",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "1",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-59-7758b1a191f8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtable_rows\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtd\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m/home/pybokeh/python33/lib/python3.3/site-packages/bs4/element.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 903\u001b[0m \"\"\"tag[key] returns the value of the 'key' attribute for the tag,\n\u001b[0;32m 904\u001b[0m and throws an exception if it's not there.\"\"\"\n\u001b[1;32m--> 905\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mattrs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 906\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 907\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m__iter__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mKeyError\u001b[0m: 1"
]
}
],
"prompt_number": 59
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "As you can see, that didn't work, I got an error. Then I tried this thinking maybe to get the 2nd \"td\" tag values, I need to call \"td\" twice maybe??"
},
{
"cell_type": "code",
"collapsed": false,
"input": "for row in table_rows:\n print(row.td.td.get_text())",
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'NoneType' object has no attribute 'get_text'",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-60-06eaa7f39a23>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtable_rows\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'get_text'"
]
}
],
"prompt_number": 60
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Again, this also doesn't work. So what should we do?<br>\nWell it turns out if you look above, previously we did: soup.find_all('tr'). But to get all the data in the \"td\" tags, we should do: soup.find_all('td')"
},
{
"cell_type": "code",
"collapsed": false,
"input": "td = soup.find_all('td')",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 61
},
{
"cell_type": "code",
"collapsed": false,
"input": "td",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 62,
"text": "[<td>2,925c</td>,\n <td>1</td>,\n <td>15</td>,\n <td></td>,\n <td>15\n</td>,\n <td>11,040c</td>,\n <td>1</td>,\n <td>30</td>,\n <td></td>,\n <td>30\n</td>,\n <td>310c</td>,\n <td>2</td>,\n <td>12</td>,\n <td></td>,\n <td>10\n</td>,\n <td>310c</td>,\n <td>4</td>,\n <td>13</td>,\n <td></td>,\n <td>10\n</td>,\n <td>6,185c</td>,\n <td>1</td>,\n <td>25</td>,\n <td></td>,\n <td>25\n</td>,\n <td>5,460c</td>,\n <td>1</td>,\n <td>5</td>,\n <td></td>,\n <td>5\n</td>,\n <td>1,600c</td>,\n <td>1</td>,\n <td>16</td>,\n <td></td>,\n <td>15\n</td>,\n <td>2,700c</td>,\n <td>1</td>,\n <td>40</td>,\n <td></td>,\n <td>40\n</td>,\n <td>370c</td>,\n <td>1</td>,\n <td>27</td>,\n <td></td>,\n <td>25\n</td>,\n <td>3,420c</td>,\n <td>1</td>,\n <td>3</td>,\n <td></td>,\n <td>3\n</td>]"
}
],
"prompt_number": 62
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Aha! This did it!<br>\nBut I don't want all that HTML markup junk, I just want the values. Furthermore, I should probably remove the \"c\" and \",\" in the prices just in case I want to do some numerical calculations on them. I also notice there is a column with no values in it. I will make them zeros instead."
},
{
"cell_type": "code",
"collapsed": false,
"input": "for data in td:\n value = data.get_text().replace('c','').replace(',','')\n # if there is no value, then make the value 0/zero\n if value == '':\n print(\"0\")\n else:\n print(value)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "2925\n1\n15\n0\n15\n\n11040\n1\n30\n0\n30\n\n310\n2\n12\n0\n10\n\n310\n4\n13\n0\n10\n\n6185\n1\n25\n0\n25\n\n5460\n1\n5\n0\n5\n\n1600\n1\n16\n0\n15\n\n2700\n1\n40\n0\n40\n\n370\n1\n27\n0\n25\n\n3420\n1\n3\n0\n3\n\n"
}
],
"prompt_number": 63
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Hmmm, why is there a blank line between the sets of 5 data rows? Looks like maybe newline character. So we'll remove that as well."
},
{
"cell_type": "code",
"collapsed": false,
"input": "for data in td:\n value = data.get_text().replace('c','').replace(',','').replace('\\n','')\n if value == '':\n print(\"0\")\n else:\n print(value)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "2925\n1\n15\n0\n15\n11040\n1\n30\n0\n30\n310\n2\n12\n0\n10\n310\n4\n13\n0\n10\n6185\n1\n25\n0\n25\n5460\n1\n5\n0\n5\n1600\n1\n16\n0\n15\n2700\n1\n40\n0\n40\n370\n1\n27\n0\n25\n3420\n1\n3\n0\n3\n"
}
],
"prompt_number": 64
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "So that we can grab certain values somehow in an orderly fashion, let's dump the values in a list so we can take advantage of list functions or list indexing."
},
{
"cell_type": "code",
"collapsed": false,
"input": "mylist = []\nfor data in td:\n value = data.get_text().replace('c','').replace(',','').replace('\\n','')\n if value == '':\n mylist.append(0)\n else:\n mylist.append(int(value))\n\nmylist",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 65,
"text": "[2925,\n 1,\n 15,\n 0,\n 15,\n 11040,\n 1,\n 30,\n 0,\n 30,\n 310,\n 2,\n 12,\n 0,\n 10,\n 310,\n 4,\n 13,\n 0,\n 10,\n 6185,\n 1,\n 25,\n 0,\n 25,\n 5460,\n 1,\n 5,\n 0,\n 5,\n 1600,\n 1,\n 16,\n 0,\n 15,\n 2700,\n 1,\n 40,\n 0,\n 40,\n 370,\n 1,\n 27,\n 0,\n 25,\n 3420,\n 1,\n 3,\n 0,\n 3]"
}
],
"prompt_number": 65
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Now we can use list indexing/slicing to grab the price values, which is the first value and skipping over the next 5 values and so forth:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "prices = mylist[0::5]\nprices",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 66,
"text": "[2925, 11040, 310, 310, 6185, 5460, 1600, 2700, 370, 3420]"
}
],
"prompt_number": 66
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Using similar logic, we can get the \"You\" column and the rest of the columns."
},
{
"cell_type": "code",
"collapsed": false,
"input": "you = mylist[1::5]\nyou",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 67,
"text": "[1, 1, 2, 4, 1, 1, 1, 1, 1, 1]"
}
],
"prompt_number": 67
},
{
"cell_type": "code",
"collapsed": false,
"input": "wh = mylist[2::5]\nwh",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 68,
"text": "[15, 30, 12, 13, 25, 5, 16, 40, 27, 3]"
}
],
"prompt_number": 68
},
{
"cell_type": "code",
"collapsed": false,
"input": "wh_diff = mylist[3::5]\nwh_diff",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 69,
"text": "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
}
],
"prompt_number": 69
},
{
"cell_type": "code",
"collapsed": false,
"input": "limit = mylist[4::5]\nlimit",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 70,
"text": "[15, 30, 10, 10, 25, 5, 15, 40, 25, 3]"
}
],
"prompt_number": 70
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Let's import the numpy package so we can take advantage of its numerical functions"
},
{
"cell_type": "code",
"collapsed": false,
"input": "import numpy as np\ntf_prices = np.array(prices)\nprint(\"The sum of all the items' prices is: \", tf_prices.sum())\nprint(\"The average of all the items' prices is: \", tf_prices.mean())",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "The sum of all the items' prices is: 34320\nThe average of all the items' prices is: 3432.0\n"
}
],
"prompt_number": 71
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Now, let's re-create the inventory table in that HTML page using a Python 3rd party package called Pandas."
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "But, before we do, we need to store the item's status in a list and the item's name in a list. I will just copy code we used above to get them and slightly modify it to dump them into a list."
},
{
"cell_type": "code",
"collapsed": false,
"input": "table_rows = table_body.find_all('tr')\n\nstatus = []\nfor row in table_rows:\n status.append(row['class'][0])\n\nstatus",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 72,
"text": "['overstocked',\n 'overstocked',\n 'overstocked',\n 'overstocked',\n 'overstocked',\n 'overstocked',\n 'overstocked',\n 'overstocked',\n 'overstocked',\n 'overstocked']"
}
],
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": "item_names = []\nfor row in table_rows:\n item_names.append(row.font.get_text())\n \nitem_names",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 73,
"text": "['Genuine Companion Cube Pin',\n 'El Jefe',\n 'Fall 2013 Acorns Crate',\n 'Fall 2013 Gourd Crate',\n 'Point and Shoot',\n 'Strange Part: Gib Kills',\n 'Strange Enforcer',\n 'Strange Medi Gun',\n 'Strange Neon Annihilator',\n 'Haunted B-ankh!']"
}
],
"prompt_number": 73
},
{
"cell_type": "code",
"collapsed": false,
"input": "import pandas as pd\n\ndf = pd.DataFrame(data={\"Name\":item_names, \"Status\":status, \"*Price\":prices, \"You\":you, \"WH\":wh, \"WH_Diff\":wh_diff, \"Limit\":limit})\n\ndf = df[[\"Name\",\"*Price\", \"You\", \"WH\", \"WH_Diff\", \"Limit\", \"Status\"]]\ndf",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Name</th>\n <th>*Price</th>\n <th>You</th>\n <th>WH</th>\n <th>WH_Diff</th>\n <th>Limit</th>\n <th>Status</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td> Genuine Companion Cube Pin</td>\n <td> 2925</td>\n <td> 1</td>\n <td> 15</td>\n <td> 0</td>\n <td> 15</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>1</th>\n <td> El Jefe</td>\n <td> 11040</td>\n <td> 1</td>\n <td> 30</td>\n <td> 0</td>\n <td> 30</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>2</th>\n <td> Fall 2013 Acorns Crate</td>\n <td> 310</td>\n <td> 2</td>\n <td> 12</td>\n <td> 0</td>\n <td> 10</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>3</th>\n <td> Fall 2013 Gourd Crate</td>\n <td> 310</td>\n <td> 4</td>\n <td> 13</td>\n <td> 0</td>\n <td> 10</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>4</th>\n <td> Point and Shoot</td>\n <td> 6185</td>\n <td> 1</td>\n <td> 25</td>\n <td> 0</td>\n <td> 25</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>5</th>\n <td> Strange Part: Gib Kills</td>\n <td> 5460</td>\n <td> 1</td>\n <td> 5</td>\n <td> 0</td>\n <td> 5</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>6</th>\n <td> Strange Enforcer</td>\n <td> 1600</td>\n <td> 1</td>\n <td> 16</td>\n <td> 0</td>\n <td> 15</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>7</th>\n <td> Strange Medi Gun</td>\n <td> 2700</td>\n <td> 1</td>\n <td> 40</td>\n <td> 0</td>\n <td> 40</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>8</th>\n <td> Strange Neon Annihilator</td>\n <td> 370</td>\n <td> 1</td>\n <td> 27</td>\n <td> 0</td>\n <td> 25</td>\n <td> overstocked</td>\n </tr>\n <tr>\n <th>9</th>\n <td> Haunted B-ankh!</td>\n <td> 3420</td>\n <td> 1</td>\n <td> 3</td>\n <td> 0</td>\n <td> 3</td>\n <td> overstocked</td>\n </tr>\n </tbody>\n</table>\n</div>",
"metadata": {},
"output_type": "pyout",
"prompt_number": 74,
"text": " Name *Price You WH WH_Diff Limit Status\n0 Genuine Companion Cube Pin 2925 1 15 0 15 overstocked\n1 El Jefe 11040 1 30 0 30 overstocked\n2 Fall 2013 Acorns Crate 310 2 12 0 10 overstocked\n3 Fall 2013 Gourd Crate 310 4 13 0 10 overstocked\n4 Point and Shoot 6185 1 25 0 25 overstocked\n5 Strange Part: Gib Kills 5460 1 5 0 5 overstocked\n6 Strange Enforcer 1600 1 16 0 15 overstocked\n7 Strange Medi Gun 2700 1 40 0 40 overstocked\n8 Strange Neon Annihilator 370 1 27 0 25 overstocked\n9 Haunted B-ankh! 3420 1 3 0 3 overstocked"
}
],
"prompt_number": 74
},
{
"cell_type": "code",
"collapsed": false,
"input": "print(\"The sum of all the items' prices is: \", df['*Price'].sum())",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "The sum of all the items' prices is: 34320\n"
}
],
"prompt_number": 75
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment