{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/selecting-and-adding-columns-to-a-dataframe-in-pandas/",
    "result": {"data":{"markdownRemark":{"html":"<div class=\"table-of-contents\">\n<ul>\n<li>\n<p><a href=\"#selecting-a-single-column\">Selecting a single column</a></p>\n</li>\n<li>\n<p><a href=\"#selecting-two-or-more-columns\">Selecting two or more columns</a></p>\n</li>\n<li>\n<p><a href=\"#changing-the-order-of-the-columns\">Changing the order of the columns</a></p>\n</li>\n<li>\n<p><a href=\"#adding-columns-to-a-dataframe\">Adding Columns to a DataFrame</a></p>\n<ul>\n<li><a href=\"#insert\">insert()</a></li>\n</ul>\n</li>\n</ul>\n</div>\n<h3 id=\"selecting-a-single-column\" style=\"position:relative;\"><a href=\"#selecting-a-single-column\" aria-label=\"selecting a single column permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Selecting a single column</h3>\n<p>Below, we have read the <del>budget.xlsx</del> file into a DataFrame.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk10\">import</span><span class=\"mtk15\"> pandas </span><span class=\"mtk10\">as</span><span class=\"mtk15\"> pd</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> pd.</span><span class=\"mtk6\">read_excel</span><span class=\"mtk15\">(“budget.xlsx”)</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"4\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 540px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/ef253ccf823c053b8b74fb353934e421/03ec9/budget.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 33%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABLklEQVQY002Qy26DMBBF+//fRjdRK5EKDI7thEcbgmfGHj+pUDfdnc3Rubpv6fVz1BJCAAQiKqXUUpxzIUYEZGaw9vV6WTzZWrvvO4BFxJzz23EcMQStTdd1nx8fN6WMMddrK4S4XC5CiEGIr2vbNO/jOPR933dd27ZN0zy37ZSdIyRKKY1Sfq/rIIQcx23btNEAUOvhEOUoAbGUknP23gshEPGUQ2BmTjFqrZ1z8+OulDqOY993RCy1IoDSipzPOaeUEWAYBkQ6ZfYekZj9TUoLdpnnuzE5p+fzx1qbUga7S3mWY4whRrBW9D38L8cYtNZE9L2uWmn2vCwLEdVaw3mKIuf+ZscYbzdJ9FdmRkTv/TxP67JorZflBGPuiBhCcI6maQI4mZm9949pIke/qo6JPYDNE0kAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Budget\"\n        title=\"Budget\"\n        src=\"/static/ef253ccf823c053b8b74fb353934e421/03ec9/budget.png\"\n        srcset=\"/static/ef253ccf823c053b8b74fb353934e421/56d15/budget.png 200w,\n/static/ef253ccf823c053b8b74fb353934e421/d9f49/budget.png 400w,\n/static/ef253ccf823c053b8b74fb353934e421/03ec9/budget.png 540w\"\n        sizes=\"(max-width: 540px) 100vw, 540px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>We can select a single column from the <del>budget</del> DataFrame using the bracket syntax:</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[</span><span class=\"mtk16\">&quot;Store Code&quot;</span><span class=\"mtk15\">]</span></span></span></code></pre>\n<p>Output:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 351px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/856803d9e29fcb3a44600e6845630b43/7ecec/storeCodeColumn.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 77%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABFUlEQVQoz5WSyW7DMAxE8///1+ZgSabkJY5tKVqS2JJDsYiNXnpoq7mQIPAwQ2BOuDzjoNahy8Frc4sxElGjAKREzL/rRDnnLWKKlNF5n7aNiFhVCZD0l07HyDkTkXNu22HBmYB6v/0b9t4fMOdMKSiDrXNHbCkBoBB2385d2zRtj5gLYGNMSukdm7GPz3MZHELYXi8iglrUIMtia63j7gxCcC7KnLU2y7K+SyJBNYU/T/N0HSciUhJq2SBiATwbrW9mLwlXjcJc4myts9YRUdu0XduVxp77rkd8QS0qxpZ1fTwfMcZ1jff7e0kpheCfy4IZf8I3Y6qK+RDGceiHq7VWKTmNk56N4LWetXOO8epyGY4ifgFb2Wx7oOQSBAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Store Code Column\"\n        title=\"Store Code Column\"\n        src=\"/static/856803d9e29fcb3a44600e6845630b43/7ecec/storeCodeColumn.png\"\n        srcset=\"/static/856803d9e29fcb3a44600e6845630b43/56d15/storeCodeColumn.png 200w,\n/static/856803d9e29fcb3a44600e6845630b43/7ecec/storeCodeColumn.png 351w\"\n        sizes=\"(max-width: 351px) 100vw, 351px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>The selected column will be extracted as a Series object.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk9\">type</span><span class=\"mtk15\">(budget[</span><span class=\"mtk16\">&quot;Store Code&quot;</span><span class=\"mtk15\">])</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">pandas.core.series.Series</span></span></span></code></pre>\n<h3 id=\"selecting-two-or-more-columns\" style=\"position:relative;\"><a href=\"#selecting-two-or-more-columns\" aria-label=\"selecting two or more columns permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Selecting two or more columns</h3>\n<p>We can select two or more columns from a DataFrame by passing a Python List inside the square brackets, where the List items are the column names that we want to select.</p>\n<p>We can select the <del>Store Code</del> &#x26; <del>City</del> columns as shown below:</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[[</span><span class=\"mtk16\">&quot;Store Code&quot;</span><span class=\"mtk15\">, </span><span class=\"mtk16\">&quot;City&quot;</span><span class=\"mtk15\">]].</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 291px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/841d727f61e648bd77529fd47afd6a50/b3b34/budgetMultipleColumns.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 62%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABXklEQVQoz5WRbW+DIBSF9/9/2l50Wau4Vrq1SLWIICAvCtXFuizbB5vs5H4AwnNP7rkPQXcDo0G2V2fHaZZg7IiKmlJyqcrqcnubQvDo+NkZO03TOC4fp4fpl7z3xhi43z0+RQAAkCZJAlTXOeda0cbRy9LrFzyOc03z3TmbpEBKpXXHmnqzeQO7/APC5+g53W6j6AXhatU5hEBp0/e9c07rDmNcXohoW4wLSmqETkJ1q/A4jpy3y1kpmQFQFOc5BSF2u3ybJoSLVfh6vWqtvfchBGsMY0xKFXxQStGG1jVZAvvRH3gYegih1toYU5XnOI5Bls0R5nn8Gm82r+hc3nOWUi3OxhhKKed88H4YBsZZRS7GrjvfZubfM0sB4YHWzeKD0GkLMtvf3XNVVdZa1/cNpfk+x7gkhBw+IEbFAe4pE3fgAeOzvYmxBsIDOhW0rt/fs9PnEYCUUL4K/1df7NivVQBW8e4AAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Multiple Columns\"\n        title=\"Multiple Columns\"\n        src=\"/static/841d727f61e648bd77529fd47afd6a50/b3b34/budgetMultipleColumns.png\"\n        srcset=\"/static/841d727f61e648bd77529fd47afd6a50/56d15/budgetMultipleColumns.png 200w,\n/static/841d727f61e648bd77529fd47afd6a50/b3b34/budgetMultipleColumns.png 291w\"\n        sizes=\"(max-width: 291px) 100vw, 291px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>Because we have selected more than one column, the output is a DataFrame.</p>\n<h3 id=\"changing-the-order-of-the-columns\" style=\"position:relative;\"><a href=\"#changing-the-order-of-the-columns\" aria-label=\"changing the order of the columns permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Changing the order of the columns</h3>\n<p>If we want the resulting DataFrame to contain the selected columns in a specific order (<em>which is different from the original order</em>), all we need to do is to make sure that the column names that we pass to the Python List are in that specific order.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[[</span><span class=\"mtk16\">&quot;City&quot;</span><span class=\"mtk15\">, </span><span class=\"mtk16\">&quot;Store Code&quot;</span><span class=\"mtk15\">]].</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 268px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9de9835aff971b2a5d178a79b8ed15b2/baf7c/budgetMultipleColumnsReversed.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 66.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABr0lEQVQoz32S6ZKbMBCE/f6vldRWJfHFudj4WBtjgw2S0DESIImUzB7JXv1TVTNfd48mPUWwTdRpJ/db26phGKwx9kXDt5pY3WvgBrgWzBozvo5DEngQhJSxPD8FQXjMssPTPowibZ6XTt4tA4Aa1ZgQRll1u4aBf8yyTZpOp7MkWaXpeul5XIgRcB+2dnAeHbauqu1mt0oSP/DjIPj1+8/pfAEAKWVDsOf5vucLKf8Z/p+MMSmLIj/nVVWdshNlXCkppRSCF0WR57nquo+2XZIWBKFsGIbL5ZymaZRuEMGve3eb7Xq1eYn8gaz7Xql2sAPnvKqq86VknLtIxrRKIYQJoW9tv3Hvh2kIPhyOnIvHKAyCcL5cXIoCQABAeS28xcz3Q1Dqi7aFIIRIJW9lWdeovF2bhqq7KKWFU9l+ltmpVbKuXche9+nqMV6nhD37ZIwmcbQ/ZsZ+cWettQTQRltrUF1h50L1TlpKiRGijI9H/SQzwfVu/xRH0XwxW8Xxj58P+dllFgLKslwuZtPFnHIxfo735K7rAARnHBPcNA1CCABap07c62go7ft+JP8FNm3lSooTR+QAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Multiple Columns\"\n        title=\"Multiple Columns\"\n        src=\"/static/9de9835aff971b2a5d178a79b8ed15b2/baf7c/budgetMultipleColumnsReversed.png\"\n        srcset=\"/static/9de9835aff971b2a5d178a79b8ed15b2/56d15/budgetMultipleColumnsReversed.png 200w,\n/static/9de9835aff971b2a5d178a79b8ed15b2/baf7c/budgetMultipleColumnsReversed.png 268w\"\n        sizes=\"(max-width: 268px) 100vw, 268px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>If the two pairs of square brackets are a bit confusing, we can assign the List to a variable and pass that variable inside the square brackets instead.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">columns </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> [</span><span class=\"mtk16\">&quot;City&quot;</span><span class=\"mtk15\">, </span><span class=\"mtk16\">&quot;Store Code&quot;</span><span class=\"mtk15\">]</span></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"2\"></span><span class=\"grvsc-source\"></span></span>\n<span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"3\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[columns].</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<h3 id=\"adding-columns-to-a-dataframe\" style=\"position:relative;\"><a href=\"#adding-columns-to-a-dataframe\" aria-label=\"adding columns to a dataframe permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Adding Columns to a DataFrame</h3>\n<p>Let’s say we want to add a column named <del>Month</del> to the <del>budget</del> DataFrame. And the column should be populated with the value <del>August</del>.</p>\n<p>We can do so using the bracket syntax as shown below:</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget[</span><span class=\"mtk16\">&quot;Month&quot;</span><span class=\"mtk15\">] </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk16\">&quot;August&quot;</span></span></span></code></pre>\n<p>Now, if we preview the <del>budget</del> DataFrame, we can see that we have the <del>Month</del> column added to the DataFrame.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 594px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/746b52046560050fd11e6b1b4c355394/d32a3/MonthColumnAdded.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 31%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA/UlEQVQY01WP227CMBBE+f8PLKBUXIIvu45jbzZ24ktwEKhV1XmdOTqawxa4MrVtm+cZAJzzrTUiIk/jODrvlNZaSaIJEUC/Y+2ICCHGw/5JzhkRpZRd9y2l6LrucrmcT2cANMYcj1/3vjfDgAi32/VTncww/MC11pzzskQE5Imu9z7n7L0vpez7bgbDPL93bXfOAaAUcmL+hUsNITBPAEje9+KRcibvY4wpJaWkcz6XklJCRKWUEIKm6c+8rmnmSWtcl+UhxLqu42jXlFpriOg8PZ/Pbduccxrgn7mUEmMMISAYY/B9dRgAIKVUa7XWElEppeTMzNaOoDUzvwDwflEydDXvxAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Month Column\"\n        title=\"Month Column\"\n        src=\"/static/746b52046560050fd11e6b1b4c355394/d32a3/MonthColumnAdded.png\"\n        srcset=\"/static/746b52046560050fd11e6b1b4c355394/56d15/MonthColumnAdded.png 200w,\n/static/746b52046560050fd11e6b1b4c355394/d9f49/MonthColumnAdded.png 400w,\n/static/746b52046560050fd11e6b1b4c355394/d32a3/MonthColumnAdded.png 594w\"\n        sizes=\"(max-width: 594px) 100vw, 594px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<h4 id=\"insert\" style=\"position:relative;\"><a href=\"#insert\" aria-label=\"insert permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>insert()</h4>\n<p>We can also add columns to a DataFrame using the <del>insert()</del> method.</p>\n<ul>\n<li>The first argument we pass to the <del>insert()</del> method is the position where we want to insert the new column.</li>\n<li>The second argument is the name of the new column that we want to add to the DataFrame.</li>\n<li>And the third argument is the value that we want the new column to be populated with.</li>\n</ul>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">insert</span><span class=\"mtk15\">(</span><span class=\"mtk5\">3</span><span class=\"mtk15\">, </span><span class=\"mtk8 mtki\">column</span><span class=\"mtk15\"> </span><span class=\"mtk12\">=</span><span class=\"mtk15\"> </span><span class=\"mtk16\">&quot;Month&quot;</span><span class=\"mtk15\">, </span><span class=\"mtk8 mtki\">value</span><span class=\"mtk12\">=</span><span class=\"mtk16\">&quot;August&quot;</span><span class=\"mtk15\">)</span></span></span></code></pre>\n<blockquote>\n<p>The <del>insert()</del> method permanently modifies the original DataFrame.</p>\n</blockquote>\n<p>Let’s preview the <del>budget</del> DataFrame.</p>\n<pre class=\"grvsc-container synthwave-84\" data-language=\"py\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"grvsc-gutter-pad\"></span><span class=\"grvsc-gutter grvsc-line-number\" aria-hidden=\"true\" data-content=\"1\"></span><span class=\"grvsc-source\"><span class=\"mtk15\">budget.</span><span class=\"mtk6\">head</span><span class=\"mtk15\">()</span></span></span></code></pre>\n<p><strong>Output:</strong></p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 593px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/18b2bce8b77e12757e3595838fba72ae/a0904/insertColumnMethod.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 30%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA+klEQVQY00XP0W7CMAyF4b3/8zFN6wa00CR2EtdBTUtid+kEmth3/+vovG1r1jQ1qXOKOA7kwaOfc1bV9BRjJCJrrQNIzAAAzlnr5nl+259U6uX01X1+BDB9P/Tn8zAMx+Pp+P0QQrDGHA7vDsAjAkDXdYD4F/+okgczjk2FiJgZEZ3DWmVibq2JCCKWWvd9b605aznxK5bgRnO9liWHEIgm7z0AShWaplJKzvM4jjnnWmsp5Xq50DT9x9E7a4yWewiRmVNKFqCUEiiqqtRqrF2Wdds2VQXnmF/Lm0Y0cO3rmjklj/i4FwICBiIRKfe792FZV3kiotvt9gtcJlCmKF967QAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Month Column Inserted\"\n        title=\"Month Column Inserted\"\n        src=\"/static/18b2bce8b77e12757e3595838fba72ae/a0904/insertColumnMethod.png\"\n        srcset=\"/static/18b2bce8b77e12757e3595838fba72ae/56d15/insertColumnMethod.png 200w,\n/static/18b2bce8b77e12757e3595838fba72ae/d9f49/insertColumnMethod.png 400w,\n/static/18b2bce8b77e12757e3595838fba72ae/a0904/insertColumnMethod.png 593w\"\n        sizes=\"(max-width: 593px) 100vw, 593px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n        decoding=\"async\"\n      />\n  </a>\n    </span></p>\n<p>We can see that the <del>Month</del> column has been added at the index position <del>0</del> and the entire column has been populated with the value <del>August</del>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n  .synthwave-84 { background-color: #262335; }\n  .synthwave-84 .mtki { font-style: italic; }\n  .synthwave-84 .mtk10 { color: #FEDE5D; }\n  .synthwave-84 .mtk15 { color: #FF7EDBFF; }\n  .synthwave-84 .mtk12 { color: #FFFFFFEE; }\n  .synthwave-84 .mtk6 { color: #36F9F6; }\n  .synthwave-84 .mtk16 { color: #FF8B39; }\n  .synthwave-84 .mtk9 { color: #FE4450; }\n  .synthwave-84 .mtk5 { color: #F97E72; }\n  .synthwave-84 .mtk8 { color: #72F1B8; }\n  .synthwave-84 .grvsc-line-highlighted::before {\n    background-color: var(--grvsc-line-highlighted-background-color, rgba(255, 255, 255, 0.1));\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, rgba(255, 255, 255, 0.5));\n  }\n</style>","frontmatter":{"title":"Selecting & Adding Columns to a DataFrame in Pandas","date":"2021-08-06"}}},"pageContext":{"slug":"/selecting-and-adding-columns-to-a-dataframe-in-pandas/","prev":{"fields":{"slug":"/renaming-columns-in-a-dataframe-in-pandas/"},"frontmatter":{"modules":null}},"next":{"fields":{"slug":"/writing-excel-files-using-pandas/"},"frontmatter":{"modules":null}}}},
    "staticQueryHashes": ["3159585216"]}