Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Back to Basics - Pandas #02
Charles De Barros
Charles De Barros

Posted on

Back to Basics - Pandas #02

The power of the 'locs'

One of the most common tasks we perform with Pandas is data indexing and selection. We do that pretty much daily.

Let's delve into the world ofPandas and explore the differences betweenloc() andiloc() operators. These two methods are essential for data manipulation in Python, especially when working withDataFrames. Knowing how to apply those two well is thekey to filtering a DataFrame efficiently. Did you get it?loc andkey? Nevermind! Let's jump in.

Selecting withloc() andiloc()

1.loc() - Label-Based Data Selection

Theloc() function is a label-based data selection method. It allows us to select rows or columns based on theirlabels (i.e., row or column names) but may also be used with a boolean array with the same length as the row axis. Some key points aboutloc():

  • We pass thename of the row or column we want to select.
  • It includes thelast element of the range specified.
  • It can acceptboolean data for filtering.
  • It's useful for selecting data based on specific conditions.

2.iloc() - Integer-Based Data Selection

Theiloc() function, on the other hand, is an integer-based data selection method. It usesinteger positions to access data but may also be used with a boolean array. Here are some aspects ofiloc() to keep in mind:

  • We specify theinteger index of the row or column we want to select.
  • Itexcludes the endpoint when slicing (similar to the Python slicing convention).
  • Likeloc[], it also acceptsboolean data for filtering.
  • It's ideal for accessing data by position.

Examples

Let's demonstrate these concepts using a sample DataFrame containing information about cars:

importpandasaspddata=pd.DataFrame({'Brand':['Ford','Hyundai','VW','Vauxhall','Ford','Hyundai','Renault','VW','Ford'],'Year':[2012,2014,2011,2015,2012,2016,2014,2018,2019],'Kms Driven':[50000,30000,60000,25000,10000,46000,31000,15000,12000],'City':['Manchester','London','Birmingham','London','Birmingham','London','Birmingham','Liverpool','Nottingham'],'Mileage':[28,27,25,26,28,29,24,21,24]})# Displaying the DataFramedata
Enter fullscreen modeExit fullscreen mode

Displaying the DataFrame above we get:

BrandYearKms DrivenCityMileage
Maruti201250000Manchester28
Hyundai201430000London27
VW201160000Birmingham25
Vauxhall201525000London26
Ford201210000Birmingham28
Hyundai201646000London29
Renault201431000Birmingham24
VW201815000Liverpool21
Ford201912000Nottingham24

Example 1: Conditional Selection Data

Let's useloc() to find Ford cars with a mileage greater than 25:

display(data.loc[(data.Brand=='Ford')&(data.Mileage>25)])
Enter fullscreen modeExit fullscreen mode

Output:

BrandYearKms DrivenCityMileage
Ford201250000Manchester28
Ford201210000Birmingham28

Example 2: Row Selection using ranges

We'll useiloc() to extract rows with indices from 2 to 5 (inclusive):

display(data.iloc[2:6])
Enter fullscreen modeExit fullscreen mode

Output:

BrandYearKms DrivenCityMileage
VW201160000Birmingham25
Vauxhall201525000London26
Ford201210000Birmingham28
Hyundai201646000London29

Summary

The Pandasloc andiloc are powerful tools for selecting and manipulating data within Pandas DataFrames and Series. Its utility ranges from simple row-and-column selections to more complex operations combined with other Pandas features likegroupby. They can be adapted to work with boolean conditions, thereby offering a flexible approach to data manipulation tasks. Masteringloc andiloc will add flexibility to any Data Analyst's toolbox.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a Data Analyst experienced on developing apps with Ruby/Ruby on Rails. I am learning to work with Excel, PowerBI and Tableau and Python for data analytics.I bake in my spare time.
  • Location
    London
  • Pronouns
    He/him
  • Work
    Data Analyst
  • Joined

More fromCharles De Barros

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp