<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Veena Prabhakaran on Medium]]></title>
        <description><![CDATA[Stories by Veena Prabhakaran on Medium]]></description>
        <link>https://medium.com/@veenaprabhakaran?source=rss-aa08e1a9fa36------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SDnHE1sMyPEp4PPnYOECBg.jpeg</url>
            <title>Stories by Veena Prabhakaran on Medium</title>
            <link>https://medium.com/@veenaprabhakaran?source=rss-aa08e1a9fa36------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 11:24:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@veenaprabhakaran/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Slicing and Dicing with Pandas]]></title>
            <link>https://medium.com/@veenaprabhakaran/slicing-and-dicing-with-pandas-8d424f2ec60b?source=rss-aa08e1a9fa36------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d424f2ec60b</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-manipulation]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[data-wrangling]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Veena Prabhakaran]]></dc:creator>
            <pubDate>Fri, 17 Apr 2020 18:28:09 GMT</pubDate>
            <atom:updated>2020-04-17T18:28:09.611Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*djqFiyYu5s7WeEoZIntYbw.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/qgadrian-317687/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=4461766">qgadrian</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=4461766">Pixab</a>ay</figcaption></figure><p>Pandas is one of the best things that has happened to the data science industry. Although the order does not matter but usually pandas is the first library that we import while solving any data analysis or machine learning problem.</p><p>It is said that data scientists spend a lot of their time cleaning and manipulating data, Pandas helps us with exactly that. Pandas is an open-source library that helps in storing and manipulating data in the most efficient way.</p><p>In this blog, I will throw some light on how to subset your dataframe.</p><h3><strong>Installation of Pandas</strong></h3><p>If you have Anaconda, type this on your terminal:</p><pre>conda install pandas</pre><p>If you do not have Anaconda, then open your command prompt and type the following:</p><pre>pip install pandas</pre><h3><strong>Loading data</strong></h3><p>We will be using the adult data from UCI Machine Learning Repository, you can save the data into your locals and call the pandas read_csv function to read the data, or you can directly read the data from the URL. The loaded data gets stored in Pandas data structure called <strong>DataFrame.</strong></p><p>The data does not contain headers(column labels), so we will be adding them explicitly as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cde270a8d63e04aef0af4a3a7677969c/href">https://medium.com/media/cde270a8d63e04aef0af4a3a7677969c/href</a></iframe><h3><strong>Copying Data</strong></h3><p>Suppose you want to create a copy of this data, this can be accomplished in two ways, either using the assignment operator ‘=’ or using the copy function.</p><pre># 1. Using assignment operator <br>ref_df = df</pre><pre># 2. Using the copy function<br>true_copy_df = df.copy()</pre><p>The difference here is that using the first method, we are only creating a reference which means that both ref_df and df refer to the same dataFrame. So after copying, if we make changes in the newly created ref_df, the changes will be reflected in the original df as well. The code below will make it more evident.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6747ba4aad894edd797f5be13d3bd322/href">https://medium.com/media/6747ba4aad894edd797f5be13d3bd322/href</a></iframe><p>If you see, an update in the ref_df changed the original df.</p><p>However, this is not the case with the copy function. Copy function creates an actual copy and not a reference hence any changes in the new df, will not impact the original df. Let us see it in action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfed12238220da1f6853538c973d2f6a/href">https://medium.com/media/dfed12238220da1f6853538c973d2f6a/href</a></iframe><p>As mentioned there is no change in the original df.</p><h3><strong>Subsetting Data</strong></h3><p>Suppose we do not want to work with the entire dataframe and we just need a part of it like specific columns or specific rows matching certain criteria. we can do this by subsetting the data. Let&#39;s get into the details.</p><p><strong>Selecting columns:</strong></p><p>In order to select specific columns we can directly use the labels for the columns as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f5a05958309eb5b8b5f2137225f5aa8/href">https://medium.com/media/4f5a05958309eb5b8b5f2137225f5aa8/href</a></iframe><p><strong>Selecting rows:</strong></p><p>For selecting rows, we need to give a range of rows that we want in outr subset.</p><pre>Syntax :<br><strong>df[start:stop]</strong></pre><p>where,<br>start -index of the first row of the slice<br>stop -index of the last row where you want to stop, without including it in the slice.</p><p>In the below example, we will be selecting rows at positions 0 to 3, but the row at the last position will not be included in the result.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1fb50c962514d3f4adbd05f718747af/href">https://medium.com/media/b1fb50c962514d3f4adbd05f718747af/href</a></iframe><p><strong>Filtering rows:</strong></p><p>Most often we would need to filter out the rows based on some conditions rather than simply selecting them based on their indices. We can give single condition or we can club multiple conditions together.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78752ab38df87c09221300f43c3d4ef3/href">https://medium.com/media/78752ab38df87c09221300f43c3d4ef3/href</a></iframe><p>Note: While filtering based on rows with categorical values, please ensure that there are no white spaces to avoid errors.</p><p><strong>Slicing dataFrame using loc and iloc:</strong></p><p>We can subset the dataFrame by selecting specific rows and columns together in the same command using loc and iloc.</p><p><strong>loc — label based indexing:<br></strong>Using the loc method, we subset the dataFrame with the row and column labels. The row labels here can be either list of row labels or a range of labels (start bound : stop bound). And the range is inclusive of the stop bound.</p><pre>Syntax:<br>loc[row labels, columns labels]</pre><p>Let us take an example, say we want to select the age and sex for the rows with labels 0,50 and 100.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d15cbcfd6a25c2ed3f4d8012a85f5b86/href">https://medium.com/media/d15cbcfd6a25c2ed3f4d8012a85f5b86/href</a></iframe><p>Please note here 0,50 and 100 are the row labels and not the rows at positions 0,50 or 100.</p><p>Let’s take another example where we will be selecting all columns of the rows with labels 1 to 5:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c01d80272470a5aaa640d33815e10937/href">https://medium.com/media/c01d80272470a5aaa640d33815e10937/href</a></iframe><p><strong>iloc — integer based indexing<br></strong>With this method we would be subsetting the data based on the row and column index which is an integer, here specifying columns labels like age, education, etc will not work, instead, we need to give its position. In python, index starts at 0.</p><p>Another thing to remember here is that, since we are dealing with index, whenever we specify ranges(start bound : stop bound), the stop bound will not be included in the result. The index of the last row will be one less than the stop bound.</p><p>Let us see some examples :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1f6c890137a52d288e975bdc6c70f6a/href">https://medium.com/media/d1f6c890137a52d288e975bdc6c70f6a/href</a></iframe><p>iloc examples with ranges, please note that the stop bound is not included in the result:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abd15616779594ea7aecfac4a966d538/href">https://medium.com/media/abd15616779594ea7aecfac4a966d538/href</a></iframe><p>Hope this blog helped you in understanding how to subset your data. In the next blog, I will take you through some more concepts in Pandas.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d424f2ec60b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>