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. 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

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

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically