avatarPython Fundamentals

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1846

Abstract

ure id="e46a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6XB9hqOmKQAWlWG8l9d_0Q.png"><figcaption></figcaption></figure><h1 id="e8dc">Section 2: Using loc for Label-Based Indexing</h1><h1 id="9cef">2.1 Selecting Rows by Label</h1><div id="adcc"><pre><span class="hljs-comment"># Selecting a specific row by label</span> row_bob = df.loc[df[<span class="hljs-string">'Name'</span>] == <span class="hljs-string">'Bob'</span>] <span class="hljs-built_in">print</span>(row_bob)</pre></div><figure id="6ee2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rfEy-LnikIFw8OYTZtxAVQ.png"><figcaption></figcaption></figure><h1 id="68dc">2.2 Selecting Rows and Columns by Label</h1><div id="cb80"><pre><span class="hljs-comment"># Selecting specific rows and columns by label</span> subset = df.loc[df[<span class="hljs-string">'Age'</span>] > <span class="hljs-number">25</span>, [<span class="hljs-string">'Name'</span>, <span class="hljs-string">'Salary'</span>]] <span class="hljs-built_in">print</span>(subset)</pre></div><figure id="473b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*u5sKXSrJ3Zj4wSvEzxWgpA.png"><figcaption></figcaption></figure><h1 id="fb48">Section 3: Using iloc for Position-Based Indexing</h1><h1 id="25a4">3.1 Selecting Rows by Index</h1><div id="58b2"><pre><span class="hljs-comment"># Selecting a specific row by index</span> row_index_2 = df.iloc[<span class="hljs-number">2</span>] <span class="hljs-built_in">print</span>(row_index_2)</pre></div><figure id="e039"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YpOMJh9vc_plBSHhDM4nbg.png"><figcaption></figcaption></figure><h1 id="12fe">3.2 Selecting Rows and Columns by Index</h1><div id="7284"><pre><span class="hljs-comment"># Selecting specific rows and columns by index</span> subset_ind

Options

ex = df.iloc[<span class="hljs-number">1</span>:<span class="hljs-number">4</span>, [<span class="hljs-number">0</span>, <span class="hljs-number">2</span>]] <span class="hljs-built_in">print</span>(subset_index)</pre></div><figure id="50e2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LKp8fhfCoP2EjZQG1RKezA.png"><figcaption></figcaption></figure><h1 id="da19">Section 4: Combining loc and iloc for Advanced Selection</h1><div id="b167"><pre><span class="hljs-comment"># Combining loc and iloc for advanced selection</span> advanced_selection = df.loc[df[<span class="hljs-string">'Age'</span>] > <span class="hljs-number">25</span>].iloc[:, <span class="hljs-number">1</span>:] <span class="hljs-built_in">print</span>(advanced_selection)</pre></div><figure id="9d03"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-oHtfNEX3pHklkPjjWd8iQ.png"><figcaption></figcaption></figure><h1 id="7b2f">Conclusion:</h1><p id="1700">Understanding the nuances of <code>loc</code> and <code>iloc</code> in Pandas is crucial for effective data manipulation. While <code>loc</code> provides label-based indexing for both rows and columns, <code>iloc</code> offers position-based indexing. By mastering these methods, you gain the flexibility to precisely select and manipulate data in your DataFrame. Whether you're dealing with labeled or positional indexing, Pandas' <code>loc</code> and <code>iloc</code> make it easy to extract the information you need. So, dive into the world of Pandas indexing and unleash the full potential of your data manipulation tasks.</p><h1 id="d874">Python Fundamentals</h1><p id="044b"><i>Thank you for your time and interest! <b>🚀 </b>You can find even more content at <a href="https://medium.com/@pythonfundamentals"><b>Python Fundamentals</b></a><b> 💫</b></i></p></article></body>

A Guide to loc and iloc in Python

How to Select and Filter Data in Python

Pandas, a powerful data manipulation library in Python, provides two essential methods for accessing and manipulating data: loc and iloc. In this guide, we'll explore the functionalities of these methods and showcase how they can be used to extract and manipulate data from a DataFrame. To illustrate the concepts, we'll use a basic example dataset.

Photo from Pexels

Section 1: Introduction to the Example Dataset

import pandas as pd

# Creating a basic example dataset
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'],
    'Age': [25, 30, 22, 35, 28],
    'Salary': [50000, 60000, 45000, 70000, 55000]
}

df = pd.DataFrame(data)
print(df)

Section 2: Using loc for Label-Based Indexing

2.1 Selecting Rows by Label

# Selecting a specific row by label
row_bob = df.loc[df['Name'] == 'Bob']
print(row_bob)

2.2 Selecting Rows and Columns by Label

# Selecting specific rows and columns by label
subset = df.loc[df['Age'] > 25, ['Name', 'Salary']]
print(subset)

Section 3: Using iloc for Position-Based Indexing

3.1 Selecting Rows by Index

# Selecting a specific row by index
row_index_2 = df.iloc[2]
print(row_index_2)

3.2 Selecting Rows and Columns by Index

# Selecting specific rows and columns by index
subset_index = df.iloc[1:4, [0, 2]]
print(subset_index)

Section 4: Combining loc and iloc for Advanced Selection

# Combining loc and iloc for advanced selection
advanced_selection = df.loc[df['Age'] > 25].iloc[:, 1:]
print(advanced_selection)

Conclusion:

Understanding the nuances of loc and iloc in Pandas is crucial for effective data manipulation. While loc provides label-based indexing for both rows and columns, iloc offers position-based indexing. By mastering these methods, you gain the flexibility to precisely select and manipulate data in your DataFrame. Whether you're dealing with labeled or positional indexing, Pandas' loc and iloc make it easy to extract the information you need. So, dive into the world of Pandas indexing and unleash the full potential of your data manipulation tasks.

Python Fundamentals

Thank you for your time and interest! 🚀 You can find even more content at Python Fundamentals 💫

Python
Data Science
Python3
Python Programming
Data Analysis
Recommended from ReadMedium