{"id":8664,"date":"2020-09-30T17:21:47","date_gmt":"2020-09-30T17:21:47","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8664"},"modified":"2020-10-02T19:39:26","modified_gmt":"2020-10-02T19:39:26","slug":"python-loc-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/pandas\/python-loc-function","title":{"rendered":"Python loc() function &#8211; Extract values from a dataset"},"content":{"rendered":"\n<p>Hey readers! In this article, we will be focusing on the functioning of <strong>Python loc() function<\/strong> in detail. So, let us get started!!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Working of Python loc() function<\/h2>\n\n\n\n<p>Python comprises various modules that have in-built functions to deal with and manipulate the data values.<\/p>\n\n\n\n<p>One such module is <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\">Pandas module<\/a>. <\/p>\n\n\n\n<p><strong>Pandas module<\/strong> enables us to handle large data sets containing a considerably huge amount of data for processing altogether.<\/p>\n\n\n\n<p>This is when <code>Python loc() function<\/code> comes into the picture. The loc() function helps us to retrieve data values from a dataset at an ease.<\/p>\n\n\n\n<p>Using the loc() function, we can access the data values fitted in the particular row or column based on the index value passed to the function.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npandas.DataFrame.loc&#x5B;index label]\n<\/pre><\/div>\n\n\n<p>We need to provide the index values for which we want the entire data to be represented in the output.<\/p>\n\n\n\n<p><strong>The index label may be one of the below values<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Single label &#8211; example: String<\/li><li>List of string<\/li><li>Slice objects with labels<\/li><li>List of an <a href=\"https:\/\/www.askpython.com\/python\/array\/python-array-declaration\" class=\"rank-math-link\">array<\/a> of labels, etc.<\/li><\/ul>\n\n\n\n<p>Thus, we can retrieve a particular record from a dataset based upon the index label using the loc() function.<\/p>\n\n\n\n<p>Note: If the passed index is not present as a label, it returns <strong>KeyError<\/strong>.<\/p>\n\n\n\n<p>Let us now focus on the implementation of the same using the below examples.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of Python loc() function<\/h2>\n\n\n\n<p>Let us first create a data frame with a set of data values using data frame in the Pandas module as shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\ndata = pd.DataFrame(&#x5B;&#x5B;1,1,1], &#x5B;4,4,4], &#x5B;7,7,7], &#x5B;10,10,10]],\n     index=&#x5B;&#039;Python&#039;, &#039;Java&#039;, &#039;C&#039;,&#039;Kotlin&#039;],\n     columns=&#x5B;&#039;RATE&#039;,&#039;EE&#039;,&#039;AA&#039;])\nprint(data)\n<\/pre><\/div>\n\n\n<p><strong>Dataframe<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n\tRATE\tEE\tAA\nPython\t1\t1\t1\nJava\t4\t4\t4\nC\t7\t7\t7\nKotlin\t10\t10\t10\n<\/pre><\/div>\n\n\n<p>Having created the data frame with a defined set of values, let us now try to retrieve a set of rows or columns having data values for a particular index as shown below:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Extract One Row from a Data frame<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(data.loc&#x5B;&#039;Python&#039;])\n<\/pre><\/div>\n\n\n<p>So, using the above command, we have extracted all the data values associated with the index label &#8216;Python&#8217;.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRATE    1\nEE      1\nAA      1\nName: Python, dtype: int64\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Extract Multiple Rows from a Data frame<\/strong><\/h3>\n\n\n\n<p>Let us now try to extract the data rows and columns associated with multiple indexes at the same time using the below command.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(data.loc&#x5B;&#x5B;&#039;Python&#039;,&#039;C&#039;]])\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n          RATE  EE  AA\nPython     1    1    1\nC          7    7    7\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Extract Range of Rows using Python loc()<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(data.loc&#x5B;&#039;Python&#039;:&#039;C&#039;])\n<\/pre><\/div>\n\n\n<p>Here, we have used the slice object as with labels to display the rows and columns associated with the labels from &#8216;Python&#8217; to &#8216;C&#8217;.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n          RATE  EE  AA\nPython     1   1   1\nJava       4   4   4\nC          7   7   7\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.<\/p>\n\n\n\n<p>For more such posts related to Python, Stay tune and till then Happy Learning!!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a class=\"rank-math-link\" href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.DataFrame.loc.html\" target=\"_blank\" rel=\"noopener\">Python pandas.loc() function &#8212; Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey readers! In this article, we will be focusing on the functioning of Python loc() function in detail. So, let us get started!! Working of Python loc() function Python comprises various modules that have in-built functions to deal with and manipulate the data values. One such module is Pandas module. Pandas module enables us to [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":9032,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94],"tags":[],"class_list":["post-8664","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pandas"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8664"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8664\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/9032"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}