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.
- Select a Single Row
- Select Multiple Rows
- Select Multiple Rows and Particular Columns
- 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.