Select Rows by Name in Pandas DataFrame using loc

The .loc[] function selects the data by labels of rows or columns. It can select a subset of rows and columns. There are many ways to use this function. 

  1. Select a Single Row
  2. Select Multiple Rows
  3. Select Multiple Rows and Particular Columns
  4. Select all the Rows With Some Particular Columns

Select a Single Row

Example : In this example code sets the “Name” column as the index for a DataFrame named ‘df’, then selects and displays the row with the index value “Stuti” using the .loc[] operator.

Python3




# Set 'Name' column as index
# on a Dataframe
df.set_index("Name", inplace = True)
 
# Using the operator .loc[]
# to select single row
result = df.loc["Stuti"]
 
# Show the dataframe
result


Output:

Age             28
City Varanasi
Salary 20000
Name: Stuti, dtype: object

Select Multiple Rows

Example : In this example code sets the “Name” column as the index for a DataFrame named ‘df’, then selects and displays rows with index values “Stuti” and “Seema” using the .loc[] operator.

Python3




# Set index on a Dataframe
df.set_index("Name",
            inplace = True)
 
# Using the operator .loc[]
# to select multiple rows
result = df.loc[["Stuti", "Seema"]]
 
# Show the dataframe
result


Output:

    Age    City    Salary
Name
Stuti 28 Varanasi 20000
Seema 32 Delhi 70000

Select Multiple Rows and Particular Columns

Syntax: Dataframe.loc[[“row1”, “row2″…], [“column1”, “column2”, “column3″…]]

Example : In this example code sets the “Name” column as the index, then selects the “City” and “Salary” columns for the rows with names “Stuti” and “Seema” in the DataFrame, displaying the result.

Python3




# Set 'Name' column as index
# on a Dataframe
df.set_index("Name", inplace = True)
 
# Using the operator .loc[] to
# select multiple rows with some
# particular columns
result = df.loc[["Stuti", "Seema"],
            ["City", "Salary"]]
 
# Show the dataframe
result


Output:

    City    Salary
Name
Stuti Varanasi 20000
Seema Delhi 70000

Select all the Rows With Some Particular Columns

We use a single colon [ : ] to select all rows and the list of columns that we want to select as given below :

Syntax: Dataframe.loc[[:, [“column1”, “column2”, “column3”]

Example : In this example code sets the “Name” column as the index and extracts the “City” and “Salary” columns into a new DataFrame named ‘result’.

Python3




# Set 'Name' column as index
# on a Dataframe
df.set_index("Name", inplace = True)
 
# Using the operator .loc[] to
# select all the rows with
# some particular columns
result = df.loc[:, ["City", "Salary"]]
 
# Show the dataframe
result


Output:

    City    Salary
Name
Stuti Varanasi 20000
Saumya Delhi 25000
Aaditya Mumbai 40000
Saumya Delhi 35000
Saumya Delhi 30000
Saumya Mumbai 20000
Aaditya Dehradun 24000
Seema Delhi 70000

Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc

Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular number of columns, a particular number of rows, and all the columns or a particular number of rows and columns each. Indexing is also known as Subset selection. 

Similar Reads

Creating a Dataframe to Select Rows & Columns in Pandas

There are various ways in which Pandas select columns by index, here we are explaining three generally used methods for select column index those that are follows....

Pandas Select Columns by Name in Pandas DataFrame using [ ]

...

Select Rows by Name in Pandas DataFrame using loc

The [ ] is used to select a column by mentioning the respective column name, we can use various way [ ] for select row Select Rows & Columns by Name or Index in Pandas DataFrame , here we are explaining some generally used method of [ ]....

Select Rows and Columns in Pandas DataFrame using iloc

...