{"id":127,"date":"2017-12-29T10:34:50","date_gmt":"2017-12-29T10:34:50","guid":{"rendered":"http:\/\/fcpython.com\/?p=127"},"modified":"2020-12-18T20:10:10","modified_gmt":"2020-12-18T20:10:10","slug":"dataframes","status":"publish","type":"post","link":"https:\/\/fcpython.com\/data-analysis\/dataframes","title":{"rendered":"Dataframes with Pandas"},"content":{"rendered":"<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>DataFrames power data analysis in Python &#8211; they allow us to use grids just like we would conventional spreadsheets. They give us labelled columns and rows, functions, filtering and many more tools to get the most insight and ease of use from our data.<\/p>\n<p>This page will introduce the creation of data frames, a few functions and tools to make selections within them. Let&#8217;s import our packages to get started (I&#8217;m sure you&#8217;ve installed them by now!).<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[1]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"kn\">import<\/span> <span class=\"nn\">pandas<\/span> <span class=\"k\">as<\/span> <span class=\"nn\">pd<\/span>\r\n<span class=\"kn\">import<\/span> <span class=\"nn\">numpy<\/span> <span class=\"k\">as<\/span> <span class=\"nn\">np<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>Our table below has a scout report on 4 different players&#8217; shooting, passing and defending skills.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[2]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"n\">PlayerList<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"s2\">\"Pagbo\"<\/span><span class=\"p\">,<\/span><span class=\"s2\">\"Grazemen\"<\/span><span class=\"p\">,<\/span><span class=\"s2\">\"Cantay\"<\/span><span class=\"p\">,<\/span><span class=\"s2\">\"Ravane\"<\/span><span class=\"p\">]<\/span>\r\n<span class=\"n\">SkillList<\/span><span class=\"o\">=<\/span><span class=\"p\">[<\/span><span class=\"s2\">\"Shooting\"<\/span><span class=\"p\">,<\/span><span class=\"s2\">\"Passing\"<\/span><span class=\"p\">,<\/span><span class=\"s2\">\"Defending\"<\/span><span class=\"p\">]<\/span>\r\n\r\n<span class=\"c1\">#For this example, we have a random number generator for our scout<\/span>\r\n<span class=\"c1\">#I wouldn't recommend this for an actual team<\/span>\r\n<span class=\"n\">ScoresArray<\/span> <span class=\"o\">=<\/span> <span class=\"n\">np<\/span><span class=\"o\">.<\/span><span class=\"n\">random<\/span><span class=\"o\">.<\/span><span class=\"n\">randint<\/span><span class=\"p\">(<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span><span class=\"mi\">10<\/span><span class=\"p\">,(<\/span><span class=\"mi\">4<\/span><span class=\"p\">,<\/span><span class=\"mi\">3<\/span><span class=\"p\">))<\/span>\r\n\r\n<span class=\"n\">df<\/span> <span class=\"o\">=<\/span> <span class=\"n\">pd<\/span><span class=\"o\">.<\/span><span class=\"n\">DataFrame<\/span><span class=\"p\">(<\/span><span class=\"n\">data<\/span><span class=\"o\">=<\/span><span class=\"n\">ScoresArray<\/span><span class=\"p\">,<\/span> <span class=\"n\">index<\/span><span class=\"o\">=<\/span><span class=\"n\">PlayerList<\/span><span class=\"p\">,<\/span> <span class=\"n\">columns<\/span><span class=\"o\">=<\/span><span class=\"n\">SkillList<\/span><span class=\"p\">)<\/span>\r\n<span class=\"n\">df<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[2]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Defending<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Pagbo<\/th>\n<td>6<\/td>\n<td>7<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<th>Grazemen<\/th>\n<td>7<\/td>\n<td>3<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<th>Cantay<\/th>\n<td>2<\/td>\n<td>9<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<th>Ravane<\/th>\n<td>2<\/td>\n<td>8<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>In this example, dataFrame needs 3 arguments for a fully labelled dataframe &#8211; data is the values that make up the body, index goes along the y axis and is the name of each row. Finally, columns runs along the x axis to name the columns.<\/p>\n<p>There are other ways to create dataFrames, but this will serve us perfectly for now.<\/p>\n<p>You&#8217;ve come a long way from lists and individual values!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<h3 id=\"Selecting-and-indexing\">Selecting and indexing<\/h3>\n<p>You can probably guess how we select individual values and groups<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[3]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"c1\">#Square brackets for columns<\/span>\r\n<span class=\"c1\">#If the result looks familiar, that's because DataFrame columns are series!<\/span>\r\n<span class=\"n\">df<\/span><span class=\"p\">[<\/span><span class=\"s1\">'Shooting'<\/span><span class=\"p\">]<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[3]:<\/div>\n<div class=\"output_text output_subarea output_execute_result\">\n<pre>Pagbo       6\r\nGrazemen    7\r\nCantay      2\r\nRavane      2\r\nName: Shooting, dtype: int32<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[4]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"c1\">#For rows, we use .loc if we use a name<\/span>\r\n<span class=\"c1\">#Turns out that DataFrame rows are also series!<\/span>\r\n<span class=\"n\">df<\/span><span class=\"o\">.<\/span><span class=\"n\">loc<\/span><span class=\"p\">[<\/span><span class=\"s1\">'Pagbo'<\/span><span class=\"p\">]<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[4]:<\/div>\n<div class=\"output_text output_subarea output_execute_result\">\n<pre>Shooting     6\r\nPassing      7\r\nDefending    3\r\nName: Pagbo, dtype: int32<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[5]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"c1\">#Or if we use a index number, .iloc<\/span>\r\n<span class=\"n\">df<\/span><span class=\"o\">.<\/span><span class=\"n\">iloc<\/span><span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">:<\/span><span class=\"mi\">3<\/span><span class=\"p\">]<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[5]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Defending<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Grazemen<\/th>\n<td>7<\/td>\n<td>3<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<th>Cantay<\/th>\n<td>2<\/td>\n<td>9<\/td>\n<td>2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<h3 id=\"Creating-and-removing-columns\/rows\">Creating and removing columns\/rows<\/h3>\n<p>DataFrames make it really easy for us to be flexible with our datasets. Let&#8217;s ask our scout for their thoughts on more players and skills.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[6]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"c1\">#Scout, what about their communication?<\/span>\r\n\r\n<span class=\"n\">df<\/span><span class=\"p\">[<\/span><span class=\"s1\">'Communication'<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">np<\/span><span class=\"o\">.<\/span><span class=\"n\">random<\/span><span class=\"o\">.<\/span><span class=\"n\">randint<\/span><span class=\"p\">(<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span><span class=\"mi\">10<\/span><span class=\"p\">,<\/span><span class=\"mi\">4<\/span><span class=\"p\">)<\/span>\r\n<span class=\"n\">df<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[6]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Defending<\/th>\n<th>Communication<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Pagbo<\/th>\n<td>6<\/td>\n<td>7<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>Grazemen<\/th>\n<td>7<\/td>\n<td>3<\/td>\n<td>3<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<th>Cantay<\/th>\n<td>2<\/td>\n<td>9<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<th>Ravane<\/th>\n<td>2<\/td>\n<td>8<\/td>\n<td>5<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>To add a new column, we refer to a new column with square brackets, give it a new name then fill it with a series. Remember, our scout uses random numbers as scouting scores.<\/p>\n<p>Our new manager doesn&#8217;t care about defending &#8211; they want these scores removed from report. The &#8216;.drop&#8217; method makes this easy:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[7]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"c1\">#axis=1 refers to columns<\/span>\r\n<span class=\"n\">df<\/span> <span class=\"o\">=<\/span> <span class=\"n\">df<\/span><span class=\"o\">.<\/span><span class=\"n\">drop<\/span><span class=\"p\">(<\/span><span class=\"s1\">'Defending'<\/span><span class=\"p\">,<\/span><span class=\"n\">axis<\/span><span class=\"o\">=<\/span><span class=\"mi\">1<\/span><span class=\"p\">)<\/span>\r\n<span class=\"n\">df<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[7]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Communication<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Pagbo<\/th>\n<td>6<\/td>\n<td>7<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>Grazemen<\/th>\n<td>7<\/td>\n<td>3<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<th>Cantay<\/th>\n<td>2<\/td>\n<td>9<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<th>Ravane<\/th>\n<td>2<\/td>\n<td>8<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>Great job adding and removing columns!<\/p>\n<p>Turns out, though, that our scout didn&#8217;t do their homework on the players&#8217; transfer fees. Grazemen is far too expensive and we need to swap him for another player &#8211; Mogez.<\/p>\n<p>For rows, we use &#8216;.drop&#8217; with the axis argument set to 0:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[8]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"n\">df<\/span> <span class=\"o\">=<\/span> <span class=\"n\">df<\/span><span class=\"o\">.<\/span><span class=\"n\">drop<\/span><span class=\"p\">(<\/span><span class=\"s1\">'Grazemen'<\/span><span class=\"p\">,<\/span><span class=\"n\">axis<\/span><span class=\"o\">=<\/span><span class=\"mi\">0<\/span><span class=\"p\">)<\/span>\r\n<span class=\"n\">df<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[8]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Communication<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Pagbo<\/th>\n<td>6<\/td>\n<td>7<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>Cantay<\/th>\n<td>2<\/td>\n<td>9<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<th>Ravane<\/th>\n<td>2<\/td>\n<td>8<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>And to add a new one, we refer to our new row with &#8216;.loc&#8217; (just like we did to refer to an existing one earlier). We then give this new row the list or series of values. Once again, we just use random numbers to fill the set here.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[9]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"n\">df<\/span><span class=\"o\">.<\/span><span class=\"n\">loc<\/span><span class=\"p\">[<\/span><span class=\"s1\">'Gomez'<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"n\">np<\/span><span class=\"o\">.<\/span><span class=\"n\">random<\/span><span class=\"o\">.<\/span><span class=\"n\">randint<\/span><span class=\"p\">(<\/span><span class=\"mi\">1<\/span><span class=\"p\">,<\/span><span class=\"mi\">10<\/span><span class=\"p\">,<\/span><span class=\"mi\">3<\/span><span class=\"p\">)<\/span>\r\n<span class=\"n\">df<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[9]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Communication<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Pagbo<\/th>\n<td>6<\/td>\n<td>7<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>Cantay<\/th>\n<td>2<\/td>\n<td>9<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<th>Ravane<\/th>\n<td>2<\/td>\n<td>8<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>Gomez<\/th>\n<td>6<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<h3 id=\"Conditonal-Selection\">Conditonal Selection<\/h3>\n<p>In our series, we used a true or false condition to select the data that we wanted to see. We use the exact same logic here. Let&#8217;s see where players&#8217; attributes are above 5:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[10]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"n\">df<\/span><span class=\"o\">&gt;<\/span><span class=\"mi\">5<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[10]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Communication<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Pagbo<\/th>\n<td>True<\/td>\n<td>True<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<th>Cantay<\/th>\n<td>False<\/td>\n<td>True<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<th>Ravane<\/th>\n<td>False<\/td>\n<td>True<\/td>\n<td>False<\/td>\n<\/tr>\n<tr>\n<th>Gomez<\/th>\n<td>True<\/td>\n<td>False<\/td>\n<td>False<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>Cool, we can see which players meet our criteria. You&#8217;ll notice that this returns a DataFrame of true or false values. Just like with series, we can use these booleans to return a DataFrame according to our criteria.<\/p>\n<p>Let&#8217;s apply this to a column (which we already know is just a series):<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[11]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"n\">df<\/span><span class=\"p\">[<\/span><span class=\"s1\">'Shooting'<\/span><span class=\"p\">]<\/span><span class=\"o\">&gt;<\/span><span class=\"mi\">5<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[11]:<\/div>\n<div class=\"output_text output_subarea output_execute_result\">\n<pre>Pagbo      True\r\nCantay    False\r\nRavane    False\r\nGomez      True\r\nName: Shooting, dtype: bool<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<p>As expected, we have a series of boolean values. If we use square brackets to select our dataframe using these, we will just get the filtered DataFrame.<\/p>\n<p>Therefore, if the coach is asking for players with great shooting skills, we can easily filter our DataFrame.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing code_cell rendered\">\n<div class=\"input\">\n<div class=\"prompt input_prompt\">In\u00a0[12]:<\/div>\n<div class=\"inner_cell\">\n<div class=\"input_area\">\n<div class=\" highlight hl-ipython3\">\n<pre><span class=\"n\">df<\/span><span class=\"p\">[<\/span><span class=\"n\">df<\/span><span class=\"p\">[<\/span><span class=\"s1\">'Shooting'<\/span><span class=\"p\">]<\/span><span class=\"o\">&gt;<\/span><span class=\"mi\">5<\/span><span class=\"p\">]<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"output_wrapper\">\n<div class=\"output\">\n<div class=\"output_area\">\n<div class=\"prompt output_prompt\">Out[12]:<\/div>\n<div class=\"output_html rendered_html output_subarea output_execute_result\">\n<div>\n<style>\n    .dataframe thead tr:only-child th {<br \/>\n        text-align: right;<br \/>\n    }<\/p>\n<p>    .dataframe thead th {<br \/>\n        text-align: left;<br \/>\n    }<\/p>\n<p>    .dataframe tbody tr th {<br \/>\n        vertical-align: top;<br \/>\n    }<br \/>\n<\/style>\n<table class=\"dataframe\" border=\"1\">\n<thead>\n<tr style=\"text-align: right;\">\n<th><\/th>\n<th>Shooting<\/th>\n<th>Passing<\/th>\n<th>Communication<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>Pagbo<\/th>\n<td>6<\/td>\n<td>7<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<th>Gomez<\/th>\n<td>6<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"cell border-box-sizing text_cell rendered\">\n<div class=\"prompt input_prompt\"><\/div>\n<div class=\"inner_cell\">\n<div class=\"text_cell_render border-box-sizing rendered_html\">\n<h2 id=\"Summary\">Summary<\/h2>\n<p>Great job getting to understand DataFrames &#8211; the tool that will underpin our analysis moving forward.<\/p>\n<p>You have created them, added new rows and columns, then filtered them according to your criteria.<\/p>\n<p>There is lots more to learn about DataFrames, so read around more articles on the topic and learn what you can from other articles that use them.<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>DataFrames power data analysis in Python &#8211; they allow us to use grids just like we would conventional spreadsheets. They give us labelled columns&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[17],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Dataframes with Pandas - FC Python<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fcpython.com\/data-analysis\/dataframes\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dataframes with Pandas - FC Python\" \/>\n<meta property=\"og:description\" content=\"DataFrames power data analysis in Python &#8211; they allow us to use grids just like we would conventional spreadsheets. They give us labelled columns&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fcpython.com\/data-analysis\/dataframes\" \/>\n<meta property=\"og:site_name\" content=\"FC Python\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-29T10:34:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-18T20:10:10+00:00\" \/>\n<meta name=\"author\" content=\"FCPythonADMIN\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@FC__Python\" \/>\n<meta name=\"twitter:site\" content=\"@FC__Python\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"FCPythonADMIN\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/fcpython.com\/data-analysis\/dataframes#article\",\"isPartOf\":{\"@id\":\"https:\/\/fcpython.com\/data-analysis\/dataframes\"},\"author\":{\"name\":\"FCPythonADMIN\",\"@id\":\"https:\/\/fcpython.com\/#\/schema\/person\/ed81e5728929acd0f3f2d9bf824a0bd0\"},\"headline\":\"Dataframes with Pandas\",\"datePublished\":\"2017-12-29T10:34:50+00:00\",\"dateModified\":\"2020-12-18T20:10:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/fcpython.com\/data-analysis\/dataframes\"},\"wordCount\":650,\"publisher\":{\"@id\":\"https:\/\/fcpython.com\/#organization\"},\"keywords\":[\"Pandas\"],\"articleSection\":[\"Data Analysis\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fcpython.com\/data-analysis\/dataframes\",\"url\":\"https:\/\/fcpython.com\/data-analysis\/dataframes\",\"name\":\"Dataframes with Pandas - FC Python\",\"isPartOf\":{\"@id\":\"https:\/\/fcpython.com\/#website\"},\"datePublished\":\"2017-12-29T10:34:50+00:00\",\"dateModified\":\"2020-12-18T20:10:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/fcpython.com\/data-analysis\/dataframes#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fcpython.com\/data-analysis\/dataframes\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fcpython.com\/data-analysis\/dataframes#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fcpython.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dataframes with Pandas\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fcpython.com\/#website\",\"url\":\"https:\/\/fcpython.com\/\",\"name\":\"FC Python\",\"description\":\"Learning Python through football\",\"publisher\":{\"@id\":\"https:\/\/fcpython.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fcpython.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/fcpython.com\/#organization\",\"name\":\"FC Python\",\"url\":\"https:\/\/fcpython.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/fcpython.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/fcpython.com\/wp-content\/uploads\/2017\/12\/Logocomp9.png\",\"contentUrl\":\"https:\/\/fcpython.com\/wp-content\/uploads\/2017\/12\/Logocomp9.png\",\"width\":981,\"height\":1049,\"caption\":\"FC Python\"},\"image\":{\"@id\":\"https:\/\/fcpython.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/twitter.com\/FC__Python\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/fcpython.com\/#\/schema\/person\/ed81e5728929acd0f3f2d9bf824a0bd0\",\"name\":\"FCPythonADMIN\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/fcpython.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7a172a6f730270fc0f8bb1a8ff958895?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7a172a6f730270fc0f8bb1a8ff958895?s=96&d=mm&r=g\",\"caption\":\"FCPythonADMIN\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dataframes with Pandas - FC Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fcpython.com\/data-analysis\/dataframes","og_locale":"en_GB","og_type":"article","og_title":"Dataframes with Pandas - FC Python","og_description":"DataFrames power data analysis in Python &#8211; they allow us to use grids just like we would conventional spreadsheets. They give us labelled columns&hellip;","og_url":"https:\/\/fcpython.com\/data-analysis\/dataframes","og_site_name":"FC Python","article_published_time":"2017-12-29T10:34:50+00:00","article_modified_time":"2020-12-18T20:10:10+00:00","author":"FCPythonADMIN","twitter_card":"summary_large_image","twitter_creator":"@FC__Python","twitter_site":"@FC__Python","twitter_misc":{"Written by":"FCPythonADMIN","Estimated reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fcpython.com\/data-analysis\/dataframes#article","isPartOf":{"@id":"https:\/\/fcpython.com\/data-analysis\/dataframes"},"author":{"name":"FCPythonADMIN","@id":"https:\/\/fcpython.com\/#\/schema\/person\/ed81e5728929acd0f3f2d9bf824a0bd0"},"headline":"Dataframes with Pandas","datePublished":"2017-12-29T10:34:50+00:00","dateModified":"2020-12-18T20:10:10+00:00","mainEntityOfPage":{"@id":"https:\/\/fcpython.com\/data-analysis\/dataframes"},"wordCount":650,"publisher":{"@id":"https:\/\/fcpython.com\/#organization"},"keywords":["Pandas"],"articleSection":["Data Analysis"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/fcpython.com\/data-analysis\/dataframes","url":"https:\/\/fcpython.com\/data-analysis\/dataframes","name":"Dataframes with Pandas - FC Python","isPartOf":{"@id":"https:\/\/fcpython.com\/#website"},"datePublished":"2017-12-29T10:34:50+00:00","dateModified":"2020-12-18T20:10:10+00:00","breadcrumb":{"@id":"https:\/\/fcpython.com\/data-analysis\/dataframes#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fcpython.com\/data-analysis\/dataframes"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/fcpython.com\/data-analysis\/dataframes#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fcpython.com\/"},{"@type":"ListItem","position":2,"name":"Dataframes with Pandas"}]},{"@type":"WebSite","@id":"https:\/\/fcpython.com\/#website","url":"https:\/\/fcpython.com\/","name":"FC Python","description":"Learning Python through football","publisher":{"@id":"https:\/\/fcpython.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fcpython.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/fcpython.com\/#organization","name":"FC Python","url":"https:\/\/fcpython.com\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/fcpython.com\/#\/schema\/logo\/image\/","url":"https:\/\/fcpython.com\/wp-content\/uploads\/2017\/12\/Logocomp9.png","contentUrl":"https:\/\/fcpython.com\/wp-content\/uploads\/2017\/12\/Logocomp9.png","width":981,"height":1049,"caption":"FC Python"},"image":{"@id":"https:\/\/fcpython.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/twitter.com\/FC__Python"]},{"@type":"Person","@id":"https:\/\/fcpython.com\/#\/schema\/person\/ed81e5728929acd0f3f2d9bf824a0bd0","name":"FCPythonADMIN","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/fcpython.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7a172a6f730270fc0f8bb1a8ff958895?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7a172a6f730270fc0f8bb1a8ff958895?s=96&d=mm&r=g","caption":"FCPythonADMIN"}}]}},"_links":{"self":[{"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/posts\/127"}],"collection":[{"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":3,"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/posts\/127\/revisions\/211"}],"wp:attachment":[{"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fcpython.com\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}