{"id":131,"date":"2026-03-02T14:30:47","date_gmt":"2026-03-02T09:30:47","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=131"},"modified":"2026-03-17T07:45:35","modified_gmt":"2026-03-17T02:45:35","slug":"indexing-and-slicing","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/","title":{"rendered":"\u00a0Indexing and Slicing"},"content":{"rendered":"\n<p>Indexing and slicing allow you to access specific elements or parts of an array. This is very important in Data Analytics because you often need to select specific rows, columns, or values from datasets.<\/p>\n\n\n\n<p>First, import NumPy:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<\/pre>\n\n\n\n<p>Create a sample array:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr = np.array([10, 20, 30, 40, 50])<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Indexing in 1D Arrays<\/h2>\n\n\n\n<p>Indexing means accessing a single element using its position.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr[0]   # First element<br>arr[2]   # Third element<\/pre>\n\n\n\n<p>Remember: Indexing starts from 0.<\/p>\n\n\n\n<p>Negative indexing:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr[-1]  # Last element<br>arr[-2]  # Second last element<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Slicing in 1D Arrays<\/h2>\n\n\n\n<p>Slicing allows you to access a range of elements.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<p>array[start:stop:step]<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr[1:4]    # Elements from index 1 to 3<br>arr[:3]     # First three elements<br>arr[2:]     # From index 2 to end<br>arr[::2]    # Every second element<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Indexing in 2D Arrays<\/h2>\n\n\n\n<p>Create a 2D array:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2 = np.array([[1, 2, 3],<br>                 [4, 5, 6],<br>                 [7, 8, 9]])<\/pre>\n\n\n\n<p>Access a specific element:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2[0, 1]   # Row 0, Column 1<\/pre>\n\n\n\n<p>Access a full row:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2[1]<\/pre>\n\n\n\n<p>Access a full column:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2[:, 2]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Slicing in 2D Arrays<\/h2>\n\n\n\n<p>Select rows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2[0:2]<\/pre>\n\n\n\n<p>Select specific rows and columns:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2[0:2, 1:3]<\/pre>\n\n\n\n<p>All rows, specific columns:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2[:, 0:2]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Boolean Indexing<\/h2>\n\n\n\n<p>Used for filtering data based on conditions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr[arr &gt; 25]<\/pre>\n\n\n\n<p>For 2D arrays:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr2[arr2 &gt; 5]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why Indexing and Slicing are Important<\/h2>\n\n\n\n<p>In Data Analytics, you use indexing and slicing to:<\/p>\n\n\n\n<p>Select specific records<br>Filter data<br>Modify values<br>Prepare data for analysis<br>Extract meaningful information<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Indexing and slicing help you access and manipulate specific parts of your dataset efficiently. Mastering these concepts is essential for working with NumPy arrays and real-world data.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Indexing and Slicing<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773715612704\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":68,"template":"","class_list":["post-131","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0Indexing and Slicing - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn NumPy indexing and slicing to access, filter, and manipulate 1D &amp; 2D arrays efficiently for Python data analysis.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Indexing and Slicing - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn NumPy indexing and slicing to access, filter, and manipulate 1D &amp; 2D arrays efficiently for Python data analysis.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T02:45:35+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/indexing-and-slicing\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/indexing-and-slicing\\\/\",\"name\":\"\u00a0Indexing and Slicing - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T09:30:47+00:00\",\"dateModified\":\"2026-03-17T02:45:35+00:00\",\"description\":\"Learn NumPy indexing and slicing to access, filter, and manipulate 1D & 2D arrays efficiently for Python data analysis.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/indexing-and-slicing\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/indexing-and-slicing\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/indexing-and-slicing\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Indexing and Slicing\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0Indexing and Slicing - One Language. Endless Possibilities","description":"Learn NumPy indexing and slicing to access, filter, and manipulate 1D & 2D arrays efficiently for Python data analysis.","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:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Indexing and Slicing - One Language. Endless Possibilities","og_description":"Learn NumPy indexing and slicing to access, filter, and manipulate 1D & 2D arrays efficiently for Python data analysis.","og_url":"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T02:45:35+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/","url":"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/","name":"\u00a0Indexing and Slicing - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T09:30:47+00:00","dateModified":"2026-03-17T02:45:35+00:00","description":"Learn NumPy indexing and slicing to access, filter, and manipulate 1D & 2D arrays efficiently for Python data analysis.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/indexing-and-slicing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Indexing and Slicing"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}