pandas.DataFrame.loc Property
This property 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]
To work with Python Pandas, we need to import the pandas library. Below is the syntax,
import pandas as pd
Python code to select rows from a DataFrame based on column values using loc property
Example 1:
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 65Example 2:
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 Californianeed an explanation for this answer? contact us directly to get an explanation for this answer