From 632aef1bc4de0dede77a9c451a35980db39f2f18 Mon Sep 17 00:00:00 2001 From: rasbt Date: Sat, 24 Jan 2015 22:25:57 -0500 Subject: [PATCH] Chaining Conditions - Using Bitwise Operators --- tutorials/things_in_pandas.ipynb | 481 ++++++++++++++++++++++--------- 1 file changed, 337 insertions(+), 144 deletions(-) diff --git a/tutorials/things_in_pandas.ipynb b/tutorials/things_in_pandas.ipynb index 44f3acf..bb71a69 100644 --- a/tutorials/things_in_pandas.ipynb +++ b/tutorials/things_in_pandas.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:ae8818183bc1fe6a58845005b18b12c4458686fa307a82a46857775364df6506" + "signature": "sha256:cf7223086a74b13d1ae2228a4c8545c401765a90cdb3eca418f18138a4afdaab" }, "nbformat": 3, "nbformat_minor": 0, @@ -95,7 +95,8 @@ " - [Filling NaN Rows](#Filling-NaN-Rows)\n", "- [Appending Rows to a DataFrame](#Appending-Rows-to-a-DataFrame)\n", "- [Sorting and Reindexing DataFrames](#Sorting-and-Reindexing-DataFrames)\n", - "- [Updating Columns](#Updating-Columns)" + "- [Updating Columns](#Updating-Columns)\n", + "- [Chaining Conditions - Using Bitwise Operators](#Chaining-Conditions---Using-Bitwise-Operators)" ] }, { @@ -806,8 +807,8 @@ "\n", "def process_player_col(text):\n", " name, rest = text.split('\\n')\n", - " position, team = rest.split(' \u2014 ')\n", - " return pd.Series([name, position, team])\n", + " position, team = [x.strip() for x in rest.split(' \u2014 ')]\n", + " return pd.Series([name, team, position])\n", "\n", "df[['player', 'team', 'position']] = df.player.apply(process_player_col)\n", "\n", @@ -854,8 +855,8 @@ " 10\n", " 10.47\n", " 209.49\n", + " Midfield\n", " Chelsea\n", - " Midfield\n", " \n", " \n", " 8\n", @@ -867,8 +868,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", " 9\n", @@ -880,8 +881,8 @@ " 11\n", " 7.50\n", " 150.01\n", + " Midfield\n", " Liverpool\n", - " Midfield\n", " \n", " \n", "\n", @@ -889,21 +890,21 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 7, + "prompt_number": 8, "text": [ " player salary games goals assists shots_on_target \\\n", "7 Cesc F\u00e0bregas 14.0 20 2 14 10 \n", "8 Saido Berahino 13.8 21 9 0 20 \n", "9 Steven Gerrard 13.8 20 5 1 11 \n", "\n", - " points_per_game points position team \n", - "7 10.47 209.49 Chelsea Midfield \n", - "8 7.02 147.43 West Brom Forward \n", - "9 7.50 150.01 Liverpool Midfield " + " points_per_game points position team \n", + "7 10.47 209.49 Midfield Chelsea \n", + "8 7.02 147.43 Forward West Brom \n", + "9 7.50 150.01 Midfield Liverpool " ] } ], - "prompt_number": 7 + "prompt_number": 8 }, { "cell_type": "markdown", @@ -985,8 +986,8 @@ " 13\n", " 10.17\n", " 132.23\n", + " Midfield\n", " Manchester United\n", - " Midfield\n", " \n", " \n", " 5\n", @@ -998,8 +999,8 @@ " 20\n", " 9.97\n", " NaN\n", + " Midfield\n", " Arsenal\n", - " Midfield\n", " \n", " \n", "\n", @@ -1007,19 +1008,19 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 8, + "prompt_number": 9, "text": [ " player salary games goals assists shots_on_target \\\n", "4 \u00c1ngel Di Mar\u00eda 15.0 13 3 NaN 13 \n", "5 Santiago Cazorla 14.8 20 4 NaN 20 \n", "\n", - " points_per_game points position team \n", - "4 10.17 132.23 Manchester United Midfield \n", - "5 9.97 NaN Arsenal Midfield " + " points_per_game points position team \n", + "4 10.17 132.23 Midfield Manchester United \n", + "5 9.97 NaN Midfield Arsenal " ] } ], - "prompt_number": 8 + "prompt_number": 9 }, { "cell_type": "markdown", @@ -1076,8 +1077,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " 1\n", @@ -1089,8 +1090,8 @@ " 17\n", " 13.05\n", " 274.04\n", + " Midfield\n", " Chelsea\n", - " Midfield\n", " \n", " \n", " 2\n", @@ -1102,8 +1103,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " 3\n", @@ -1115,8 +1116,8 @@ " 19\n", " 10.99\n", " 197.91\n", + " Midfield\n", " Manchester City\n", - " Midfield\n", " \n", " \n", " 6\n", @@ -1128,8 +1129,8 @@ " 11\n", " 10.35\n", " 155.26\n", + " Midfield\n", " Manchester City\n", - " Midfield\n", " \n", " \n", " 7\n", @@ -1141,8 +1142,8 @@ " 10\n", " 10.47\n", " 209.49\n", + " Midfield\n", " Chelsea\n", - " Midfield\n", " \n", " \n", " 8\n", @@ -1154,8 +1155,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", " 9\n", @@ -1167,8 +1168,8 @@ " 11\n", " 7.50\n", " 150.01\n", + " Midfield\n", " Liverpool\n", - " Midfield\n", " \n", " \n", "\n", @@ -1176,7 +1177,7 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 9, + "prompt_number": 10, "text": [ " player salary games goals assists shots_on_target \\\n", "0 Sergio Ag\u00fcero 19.2 16 14 3 34 \n", @@ -1188,19 +1189,19 @@ "8 Saido Berahino 13.8 21 9 0 20 \n", "9 Steven Gerrard 13.8 20 5 1 11 \n", "\n", - " points_per_game points position team \n", - "0 13.12 209.98 Manchester City Forward \n", - "1 13.05 274.04 Chelsea Midfield \n", - "2 11.19 223.86 Arsenal Forward \n", - "3 10.99 197.91 Manchester City Midfield \n", - "6 10.35 155.26 Manchester City Midfield \n", - "7 10.47 209.49 Chelsea Midfield \n", - "8 7.02 147.43 West Brom Forward \n", - "9 7.50 150.01 Liverpool Midfield " + " points_per_game points position team \n", + "0 13.12 209.98 Forward Manchester City \n", + "1 13.05 274.04 Midfield Chelsea \n", + "2 11.19 223.86 Forward Arsenal \n", + "3 10.99 197.91 Midfield Manchester City \n", + "6 10.35 155.26 Midfield Manchester City \n", + "7 10.47 209.49 Midfield Chelsea \n", + "8 7.02 147.43 Forward West Brom \n", + "9 7.50 150.01 Midfield Liverpool " ] } ], - "prompt_number": 9 + "prompt_number": 10 }, { "cell_type": "markdown", @@ -1260,8 +1261,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " 1\n", @@ -1273,8 +1274,8 @@ " 17\n", " 13.05\n", " 274.04\n", + " Midfield\n", " Chelsea\n", - " Midfield\n", " \n", " \n", " 2\n", @@ -1286,8 +1287,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " 3\n", @@ -1299,8 +1300,8 @@ " 19\n", " 10.99\n", " 197.91\n", + " Midfield\n", " Manchester City\n", - " Midfield\n", " \n", " \n", " 4\n", @@ -1312,8 +1313,8 @@ " 13\n", " 10.17\n", " 132.23\n", + " Midfield\n", " Manchester United\n", - " Midfield\n", " \n", " \n", " 5\n", @@ -1325,8 +1326,8 @@ " 20\n", " 9.97\n", " 0.00\n", + " Midfield\n", " Arsenal\n", - " Midfield\n", " \n", " \n", " 6\n", @@ -1338,8 +1339,8 @@ " 11\n", " 10.35\n", " 155.26\n", + " Midfield\n", " Manchester City\n", - " Midfield\n", " \n", " \n", " 7\n", @@ -1351,8 +1352,8 @@ " 10\n", " 10.47\n", " 209.49\n", + " Midfield\n", " Chelsea\n", - " Midfield\n", " \n", " \n", " 8\n", @@ -1364,8 +1365,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", " 9\n", @@ -1377,8 +1378,8 @@ " 11\n", " 7.50\n", " 150.01\n", + " Midfield\n", " Liverpool\n", - " Midfield\n", " \n", " \n", "\n", @@ -1386,7 +1387,7 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 10, + "prompt_number": 11, "text": [ " player salary games goals assists shots_on_target \\\n", "0 Sergio Ag\u00fcero 19.2 16 14 3 34 \n", @@ -1400,21 +1401,21 @@ "8 Saido Berahino 13.8 21 9 0 20 \n", "9 Steven Gerrard 13.8 20 5 1 11 \n", "\n", - " points_per_game points position team \n", - "0 13.12 209.98 Manchester City Forward \n", - "1 13.05 274.04 Chelsea Midfield \n", - "2 11.19 223.86 Arsenal Forward \n", - "3 10.99 197.91 Manchester City Midfield \n", - "4 10.17 132.23 Manchester United Midfield \n", - "5 9.97 0.00 Arsenal Midfield \n", - "6 10.35 155.26 Manchester City Midfield \n", - "7 10.47 209.49 Chelsea Midfield \n", - "8 7.02 147.43 West Brom Forward \n", - "9 7.50 150.01 Liverpool Midfield " + " points_per_game points position team \n", + "0 13.12 209.98 Forward Manchester City \n", + "1 13.05 274.04 Midfield Chelsea \n", + "2 11.19 223.86 Forward Arsenal \n", + "3 10.99 197.91 Midfield Manchester City \n", + "4 10.17 132.23 Midfield Manchester United \n", + "5 9.97 0.00 Midfield Arsenal \n", + "6 10.35 155.26 Midfield Manchester City \n", + "7 10.47 209.49 Midfield Chelsea \n", + "8 7.02 147.43 Forward West Brom \n", + "9 7.50 150.01 Midfield Liverpool " ] } ], - "prompt_number": 10 + "prompt_number": 11 }, { "cell_type": "markdown", @@ -1487,8 +1488,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", " 9 \n", @@ -1500,8 +1501,8 @@ " 11\n", " 7.50\n", " 150.01\n", + " Midfield\n", " Liverpool\n", - " Midfield\n", " \n", " \n", " 10\n", @@ -1513,7 +1514,7 @@ " NaN\n", " NaN\n", " NaN\n", - " NaN\n", + " NaN\n", " NaN\n", " \n", " \n", @@ -1522,21 +1523,21 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 11, + "prompt_number": 12, "text": [ " player salary games goals assists shots_on_target \\\n", "8 Saido Berahino 13.8 21 9 0 20 \n", "9 Steven Gerrard 13.8 20 5 1 11 \n", "10 NaN NaN NaN NaN NaN NaN \n", "\n", - " points_per_game points position team \n", - "8 7.02 147.43 West Brom Forward \n", - "9 7.50 150.01 Liverpool Midfield \n", - "10 NaN NaN NaN NaN " + " points_per_game points position team \n", + "8 7.02 147.43 Forward West Brom \n", + "9 7.50 150.01 Midfield Liverpool \n", + "10 NaN NaN NaN NaN " ] } ], - "prompt_number": 11 + "prompt_number": 12 }, { "cell_type": "code", @@ -1581,8 +1582,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", " 9 \n", @@ -1594,8 +1595,8 @@ " 11\n", " 7.50\n", " 150.01\n", + " Midfield\n", " Liverpool\n", - " Midfield\n", " \n", " \n", " 10\n", @@ -1607,7 +1608,7 @@ " NaN\n", " NaN\n", " NaN\n", - " NaN\n", + " NaN\n", " NaN\n", " \n", " \n", @@ -1616,21 +1617,21 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 12, + "prompt_number": 13, "text": [ " player salary games goals assists shots_on_target \\\n", "8 Saido Berahino 13.8 21 9 0 20 \n", "9 Steven Gerrard 13.8 20 5 1 11 \n", "10 New Player 12.3 NaN NaN NaN NaN \n", "\n", - " points_per_game points position team \n", - "8 7.02 147.43 West Brom Forward \n", - "9 7.50 150.01 Liverpool Midfield \n", - "10 NaN NaN NaN NaN " + " points_per_game points position team \n", + "8 7.02 147.43 Forward West Brom \n", + "9 7.50 150.01 Midfield Liverpool \n", + "10 NaN NaN NaN NaN " ] } ], - "prompt_number": 12 + "prompt_number": 13 }, { "cell_type": "markdown", @@ -1697,8 +1698,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " 2\n", @@ -1710,8 +1711,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " 8\n", @@ -1723,8 +1724,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", " 1\n", @@ -1736,8 +1737,8 @@ " 17\n", " 13.05\n", " 274.04\n", + " Midfield\n", " Chelsea\n", - " Midfield\n", " \n", " \n", " 3\n", @@ -1749,8 +1750,8 @@ " 19\n", " 10.99\n", " 197.91\n", + " Midfield\n", " Manchester City\n", - " Midfield\n", " \n", " \n", "\n", @@ -1758,7 +1759,7 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 13, + "prompt_number": 14, "text": [ " player salary games goals assists shots_on_target \\\n", "0 Sergio Ag\u00fcero 19.2 16 14 3 34 \n", @@ -1767,16 +1768,16 @@ "1 Eden Hazard 18.9 21 8 4 17 \n", "3 Yaya Tour\u00e9 16.6 18 7 1 19 \n", "\n", - " points_per_game points position team \n", - "0 13.12 209.98 Manchester City Forward \n", - "2 11.19 223.86 Arsenal Forward \n", - "8 7.02 147.43 West Brom Forward \n", - "1 13.05 274.04 Chelsea Midfield \n", - "3 10.99 197.91 Manchester City Midfield " + " points_per_game points position team \n", + "0 13.12 209.98 Forward Manchester City \n", + "2 11.19 223.86 Forward Arsenal \n", + "8 7.02 147.43 Forward West Brom \n", + "1 13.05 274.04 Midfield Chelsea \n", + "3 10.99 197.91 Midfield Manchester City " ] } ], - "prompt_number": 13 + "prompt_number": 14 }, { "cell_type": "code", @@ -1820,8 +1821,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " 2\n", @@ -1833,8 +1834,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " 3\n", @@ -1846,8 +1847,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", " 4\n", @@ -1859,8 +1860,8 @@ " 17\n", " 13.05\n", " 274.04\n", + " Midfield\n", " Chelsea\n", - " Midfield\n", " \n", " \n", " 5\n", @@ -1872,8 +1873,8 @@ " 19\n", " 10.99\n", " 197.91\n", + " Midfield\n", " Manchester City\n", - " Midfield\n", " \n", " \n", "\n", @@ -1881,7 +1882,7 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 14, + "prompt_number": 15, "text": [ " player salary games goals assists shots_on_target \\\n", "1 Sergio Ag\u00fcero 19.2 16 14 3 34 \n", @@ -1890,16 +1891,16 @@ "4 Eden Hazard 18.9 21 8 4 17 \n", "5 Yaya Tour\u00e9 16.6 18 7 1 19 \n", "\n", - " points_per_game points position team \n", - "1 13.12 209.98 Manchester City Forward \n", - "2 11.19 223.86 Arsenal Forward \n", - "3 7.02 147.43 West Brom Forward \n", - "4 13.05 274.04 Chelsea Midfield \n", - "5 10.99 197.91 Manchester City Midfield " + " points_per_game points position team \n", + "1 13.12 209.98 Forward Manchester City \n", + "2 11.19 223.86 Forward Arsenal \n", + "3 7.02 147.43 Forward West Brom \n", + "4 13.05 274.04 Midfield Chelsea \n", + "5 10.99 197.91 Midfield Manchester City " ] } ], - "prompt_number": 14 + "prompt_number": 15 }, { "cell_type": "markdown", @@ -1967,8 +1968,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " 2\n", @@ -1980,8 +1981,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " 3\n", @@ -1993,8 +1994,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", "\n", @@ -2002,21 +2003,21 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 15, + "prompt_number": 16, "text": [ " player salary games goals assists shots_on_target \\\n", "1 Sergio Ag\u00fcero 20 16 14 3 34 \n", "2 Alexis S\u00e1nchez 15 0 12 7 29 \n", "3 Saido Berahino 13.8 21 9 0 20 \n", "\n", - " points_per_game points position team \n", - "1 13.12 209.98 Manchester City Forward \n", - "2 11.19 223.86 Arsenal Forward \n", - "3 7.02 147.43 West Brom Forward " + " points_per_game points position team \n", + "1 13.12 209.98 Forward Manchester City \n", + "2 11.19 223.86 Forward Arsenal \n", + "3 7.02 147.43 Forward West Brom " ] } ], - "prompt_number": 15 + "prompt_number": 16 }, { "cell_type": "markdown", @@ -2080,8 +2081,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " Alexis S\u00e1nchez\n", @@ -2092,8 +2093,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " Saido Berahino\n", @@ -2104,8 +2105,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", "\n", @@ -2113,7 +2114,7 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 16, + "prompt_number": 17, "text": [ " salary games goals assists shots_on_target \\\n", "player \n", @@ -2121,15 +2122,15 @@ "Alexis S\u00e1nchez 17.6 0 12 7 29 \n", "Saido Berahino 13.8 21 9 0 20 \n", "\n", - " points_per_game points position team \n", - "player \n", - "Sergio Ag\u00fcero 13.12 209.98 Manchester City Forward \n", - "Alexis S\u00e1nchez 11.19 223.86 Arsenal Forward \n", - "Saido Berahino 7.02 147.43 West Brom Forward " + " points_per_game points position team \n", + "player \n", + "Sergio Ag\u00fcero 13.12 209.98 Forward Manchester City \n", + "Alexis S\u00e1nchez 11.19 223.86 Forward Arsenal \n", + "Saido Berahino 7.02 147.43 Forward West Brom " ] } ], - "prompt_number": 16 + "prompt_number": 17 }, { "cell_type": "markdown", @@ -2190,8 +2191,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " Alexis S\u00e1nchez\n", @@ -2202,8 +2203,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " Saido Berahino\n", @@ -2214,8 +2215,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", "\n", @@ -2223,7 +2224,7 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 17, + "prompt_number": 18, "text": [ " salary games goals assists shots_on_target \\\n", "player \n", @@ -2231,15 +2232,15 @@ "Alexis S\u00e1nchez 15 0 12 7 29 \n", "Saido Berahino 13.8 21 9 0 20 \n", "\n", - " points_per_game points position team \n", - "player \n", - "Sergio Ag\u00fcero 13.12 209.98 Manchester City Forward \n", - "Alexis S\u00e1nchez 11.19 223.86 Arsenal Forward \n", - "Saido Berahino 7.02 147.43 West Brom Forward " + " points_per_game points position team \n", + "player \n", + "Sergio Ag\u00fcero 13.12 209.98 Forward Manchester City \n", + "Alexis S\u00e1nchez 11.19 223.86 Forward Arsenal \n", + "Saido Berahino 7.02 147.43 Forward West Brom " ] } ], - "prompt_number": 17 + "prompt_number": 18 }, { "cell_type": "markdown", @@ -2290,8 +2291,8 @@ " 34\n", " 13.12\n", " 209.98\n", + " Forward\n", " Manchester City\n", - " Forward\n", " \n", " \n", " 1\n", @@ -2303,8 +2304,8 @@ " 29\n", " 11.19\n", " 223.86\n", + " Forward\n", " Arsenal\n", - " Forward\n", " \n", " \n", " 2\n", @@ -2316,8 +2317,8 @@ " 20\n", " 7.02\n", " 147.43\n", + " Forward\n", " West Brom\n", - " Forward\n", " \n", " \n", "\n", @@ -2325,21 +2326,213 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 18, + "prompt_number": 19, "text": [ " player salary games goals assists shots_on_target \\\n", "0 Sergio Ag\u00fcero 20 16 14 3 34 \n", "1 Alexis S\u00e1nchez 15 0 12 7 29 \n", "2 Saido Berahino 13.8 21 9 0 20 \n", "\n", - " points_per_game points position team \n", - "0 13.12 209.98 Manchester City Forward \n", - "1 11.19 223.86 Arsenal Forward \n", - "2 7.02 147.43 West Brom Forward " + " points_per_game points position team \n", + "0 13.12 209.98 Forward Manchester City \n", + "1 11.19 223.86 Forward Arsenal \n", + "2 7.02 147.43 Forward West Brom " ] } ], - "prompt_number": 18 + "prompt_number": 19 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chaining Conditions - Using Bitwise Operators" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[[back to section overview](#Sections)]" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Selecting only those players that either playing for Arsenal or Chelsea\n", + "\n", + "df[ (df['team'] == 'Arsenal') | (df['team'] == 'Chelsea') ]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
playersalarygamesgoalsassistsshots_on_targetpoints_per_gamepointspositionteam
1 Alexis S\u00e1nchez 15 0 12 7 29 11.19 223.86 Forward Arsenal
3 Eden Hazard 18.9 21 8 4 17 13.05 274.04 Midfield Chelsea
7 Santiago Cazorla 14.8 20 4 0 20 9.97 0.00 Midfield Arsenal
9 Cesc F\u00e0bregas 14.0 20 2 14 10 10.47 209.49 Midfield Chelsea
\n", + "
" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 20, + "text": [ + " player salary games goals assists shots_on_target \\\n", + "1 Alexis S\u00e1nchez 15 0 12 7 29 \n", + "3 Eden Hazard 18.9 21 8 4 17 \n", + "7 Santiago Cazorla 14.8 20 4 0 20 \n", + "9 Cesc F\u00e0bregas 14.0 20 2 14 10 \n", + "\n", + " points_per_game points position team \n", + "1 11.19 223.86 Forward Arsenal \n", + "3 13.05 274.04 Midfield Chelsea \n", + "7 9.97 0.00 Midfield Arsenal \n", + "9 10.47 209.49 Midfield Chelsea " + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Selecting forwards from Arsenal only\n", + "\n", + "df[ (df['team'] == 'Arsenal') & (df['position'] == 'Forward') ]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "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", + "
playersalarygamesgoalsassistsshots_on_targetpoints_per_gamepointspositionteam
1 Alexis S\u00e1nchez 15 0 12 7 29 11.19 223.86 Forward Arsenal
\n", + "
" + ], + "metadata": {}, + "output_type": "pyout", + "prompt_number": 22, + "text": [ + " player salary games goals assists shots_on_target \\\n", + "1 Alexis S\u00e1nchez 15 0 12 7 29 \n", + "\n", + " points_per_game points position team \n", + "1 11.19 223.86 Forward Arsenal " + ] + } + ], + "prompt_number": 22 } ], "metadata": {}