Change language

Python | Pandas Extracting Strings with .loc []

| |

Pandas provide a unique method for extracting strings from a data frame. DataFrame.loc† — it is a method that only accepts index labels and returns a string or dataframe if the index label exists in the caller’s dataframe.

Syntax: pandas.DataFrame.loc [ ]

Parameters:
Index label: String or list of string of index label of rows

Return type: Data frame or Series depending on parameters

To load the CSV used in the code, click here.

Example # 1: Fetching one row

In this example, the Name column is created as an index column and then two separate rows are fetched one after the other as rows using the row index label.

# import pandas package

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( "nba .csv " , index_col = " Name " )

 
# extracting a string using loc

first = data.loc [ "Avery Bradley" ]

second = data.loc [ "RJ Hunter" ]

 

 

print (first, "" , second)

Output:
As shown in the output image, there were two series were returned since there was only one parameter both times. 

Example # 2: multiple parameters

In this example, the Name column is created as an index column and then two separate rows are retrieved at the same time by passing a list as a parameter.

# import pandas package

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( "nba.csv" , index_col = "Name" )

 
# retrieve rows using the loc method

rows = data.loc [[ " Avery Bradley " , " RJ Hunter " ]]

  
# checking the data type of strings

print ( type (rows))

  
# display
rows

Output:
As shown in the output image, this time the return type is a data frame. Both lines were retrieved and displayed as a new data frame. 

Example # 3: Retrieving multiple rows with the same index

In this example, the command name is created as an index column and one command name is passed to the .loc method to check if all values ​​with the same name were returned commands.

# pandas package import

import pandas as pd

 
# create a data frame from CSV file

data = pd. read_csv ( "nba.csv" , index_col = "Team" )

 
# extracting strings using loc method

rows = data.loc [ "Utah Jazz" ]

 
# checking the data type of strings

print ( type (rows))

 
# display
rows

Output:
As shown in the output image, all lines with the team name "Utah Jazz" were returned as a data frame. 

Example # 4: extracting lines between two index marks

This example passes two row index marks and returns all rows that fall between those two index marks (both index marks are included).

# pandas package import

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( "nba .csv " , index_col = " Name " )

  
# retrieve rows using loc method

rows = data.loc [ "Avery Bradley" : "Isaiah Thomas" ]

 
# checking the data type of strings

print ( type (rows))

 
# display
rows

Output:
As shown in the output image, all lines falling between two missing index marks , are returned in the form of a data frame. 

Python | Pandas Extracting Strings with .loc [] File handling: Questions

Python | Pandas Extracting Strings with .loc [] Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method