JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Get the index of rows where a column matches a specific value in Pandas

Author:JIYIK Last Updated:2025/05/01 Views:

This article demonstrates how to get the indices of rows that match a certain condition in Pandas.

The necessity of finding the index of the rows is important in feature engineering. These skills are useful in removing outliers or abnormal values ​​in a Dataframe. The index, also known as the row labels, can be found in Pandas using several functions. In the following example, we will work with a DataFrame created using the following code snippet.

import pandas as pd
import numpy as np

np.random.seed(0)

df = pd.DataFrame(np.random.randint(1, 20, size=(20, 4)), columns=list("ABCD"))

print(df)

Output:

     A   B   C   D
0   13  16   1   4
1    4   8  10  19
2    5   7  13   2
3    7   8  15  18
4    6  14   9  10
5   17   6  16  16
6    1  19   4  18
7   15   8   1   2
8   10   1  11   4
9   12  19   3   1
10   1   5   6   7
11   9  18  16   5
12  10  11   2   2
13   8  10   4   7
14  12  15  19   1
15  15   4  13  11
16  12   5   7   5
17  16   4  13   5
18   9  15  16   4
19  16  14  17  18

Get the index of rows containing integers/floats in Pandas

pandas.DataFrame.locFunction can access rows and columns by their labels/names. It directly returns the rows matching the given boolean condition passed as label. Note df.locthe square brackets around in the snippet.

import pandas as pd
import numpy as np

np.random.seed(0)

df = pd.DataFrame(np.random.randint(1, 20, size=(20, 4)), columns=list("ABCD"))

print(df.loc[df["B"] == 19])

The rows corresponding to the Boolean condition will be returned in the output in Dataframe format.

Output:

    A   B  C   D
6   1  19  4  18
9  12  19  3   1

Multiple conditions can be chained together and applied together in the function as shown below. This helps to isolate rows based on a specific condition.

import pandas as pd
import numpy as np

np.random.seed(0)

df = pd.DataFrame(np.random.randint(1, 20, size=(20, 4)), columns=list("ABCD"))

print(df.loc[(df["B"] == 19) | (df["C"] == 19)])

Output:

     A   B   C   D
6    1  19   4  18
9   12  19   3   1
14  12  15  19   1

pandas.DataFrame.index()Get the index of the row with

pandas.DataFrame.index()This is the simplest way to implement if you want to find only the matching indices of a DataFrame that satisfy a boolean condition passed as an argument .

import pandas as pd
import numpy as np

np.random.seed(0)

df = pd.DataFrame(np.random.randint(1, 20, size=(20, 4)), columns=list("ABCD"))

print(df.index[df["B"] == 19].tolist())

In the above code snippet, the rows Ain column == 1that match the Boolean condition are returned as output as shown below.

Output:

[6, 9]

The reason we tolist()put index()after the method is to Indexconvert into a list, otherwise, the result is of Int64Indexdata type.

Int64Index([6, 9], dtype='int64'

It is also possible to retrieve only the index based on multiple conditions. This code can be written as follows.

import pandas as pd
import numpy as np

np.random.seed(0)

df = pd.DataFrame(np.random.randint(1, 20, size=(20, 4)), columns=list("ABCD"))

print(df.index[(df["B"] == 19) | (df["C"] == 19)].tolist())

Output:

[6, 9, 14]

Get the index of rows containing a string in Pandas

String values ​​can be matched based on two methods. Both methods shown in the previous section can be used, except the condition changes.

In the following examples, we will use the following snippet.

import pandas as pd

df = pd.DataFrame(
    {
        "Name": ["blue", "delta", "echo", "charlie", "alpha"],
        "Type": ["Raptors", "Raptors", "Raptors", "Raptors", "Tyrannosaurus rex"],
    }
)

print(df)

Output:

      Name               Type
0     blue            Raptors
1    delta            Raptors
2     echo            Raptors
3  charlie            Raptors
4    alpha  Tyrannosaurus rex

Get the index of the row with an exact string match

The equality condition used in the previous section can be used to find exact string matches in a Dataframe. Let's find two strings.

import pandas as pd

df = pd.DataFrame(
    {
        "Name": ["blue", "delta", "echo", "charlie", "alpha"],
        "Type": ["Raptors", "Raptors", "Raptors", "Raptors", "Tyrannosaurus rex"],
    }
)

print(df.index[(df["Name"] == "blue")].tolist())
print("\n")
print(df.loc[df["Name"] == "blue"])
print("\n")
print(df.loc[(df["Name"] == "charlie") & (df["Type"] == "Raptors")])

Output:

[0]

   Name     Type
0  blue  Raptors

      Name     Type
3  charlie  Raptors

As shown above, both the index and the rows that meet the criteria can be received.

Get the index of the row with partial string matching condition

By str.containschaining a DataFrame with a function, you can partially match string values. In the following example, we will search for strings in character and alphaha .

import pandas as pd

df = pd.DataFrame(
    {
        "Name": ["blue", "delta", "echo", "charlie", "alpha"],
        "Type": ["Raptors", "Raptors", "Raptors", "Raptors", "Tyrannosaurus rex"],
    }
)

print(df.index[df["Name"].str.contains("ha")].tolist())
print("\n")
print(df.loc[df["Name"].str.contains("ha")])
print("\n")
print(df.loc[(df["Name"].str.contains("ha")) & (df["Type"].str.contains("Rex"))])

Output:

[3, 4]

      Name               Type
3  charlie            Raptors
4    alpha  Tyrannosaurus rex


    Name               Type
4  alpha  Tyrannosaurus rex

This function is useful for performing partial string matching on multiple columns of a DataFrame.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:197 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:191 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial