How to select rows from a DataFrame based on column values?

In this tutorial, we will learn how to select rows from a DataFrame based on column values using DataFrane.loc property? By Pranit Sharma Last updated : April 10, 2023

Introduction

Columns are the different fields that contain their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. In pandas, we might need to access rows by specific column values. It can be performed using the pandas.DataFrame.loc property.

Select rows from a DataFrame based on column values

To select rows from a DataFrame, we use pandas.DataFrame.loc property which is primarily label-based and used to access a group of rows and columns by label(s) or a boolean array.

Syntax

property DataFrame.loc

Let us begin with an example to understand how we can select a row based on its column value.

DataFrame.loc[df['col-name'] == value]

Example 1: Select rows from a DataFrame based on column values

# Importing pandas package
import pandas as pd

# Creating a dictionary of student marks
d = {
    "Peter":[65,70,70,75],
    "Harry":[45,56,66,66],
    "Tom":[67,87,65,53],
    "John":[56,78,65,64]
}

# Now, create DataFrame and assign index name as subject names
df = pd.DataFrame(d,index=["Maths","Physics","Chemistry","English"])

# Printing  the DataFrame
print("DataFrame is:")
print(df)

# Selecting a row value, 
# where column name is "Harry"
# and it's value is 66
print(df.loc[df['Harry']==66],"\n")

# Selecting a row value, 
# where column name is "Peter" 
# and it's value is 70
print(df.loc[df['Peter']==70],"\n")

Output

DataFrame is:
           Peter  Harry  Tom  John
Maths         65     45   67    56
Physics       70     56   87    78
Chemistry     70     66   65    65
English       75     66   53    64
           Peter  Harry  Tom  John
Chemistry     70     66   65    65
English       75     66   53    64 

           Peter  Harry  Tom  John
Physics       70     56   87    78
Chemistry     70     66   65    65 

Example 2: Select rows from a DataFrame based on column values

# Importing pandas package
import pandas as pd

# Creating DataFrame
df = pd.DataFrame({
    'Name': ['Alvin', 'Alex', 'Peter'],
    'Age': [21, 25, 30],
    'City':['New York', 'Washington', 'California']
})

# Printing the DataFrame
print("DataFrame is:")
print(df)

# Printing row, where
# Column name is "Name", and Value is "Peter"
print("\nResult:")
print(df.loc[df['Name'] == 'Peter'])

Output

DataFrame is:
    Name  Age        City
0  Alvin   21    New York
1   Alex   25  Washington
2  Peter   30  California

Result:
    Name  Age        City
2  Peter   30  California

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.