Jump to content

Pandas Filter by Column Value


Linux Hint

Recommended Posts

The Pandas library supports various methods for performing data selection techniques in Python. Sometimes, we must select only specific parts of the data that meet certain criteria/conditions. This selection and filtration make the data easier to understand. Data filtration is normally performed in Pandas DataFrame using the label and position of rows and columns. To filter data based on the columns, several methods are utilized in Python.

This blog provides a detailed tutorial on filtering Pandas DataFrame based on the value of the column using the below content:

Method 1: Filter Pandas DataFrame by Column Value Using “df.loc[]”

The “df.loc()” method selects rows and columns of specified Pandas DataFrame using the label’s value. This can also filter DataFrame based on the column’s value. Let’s understand it via the following examples:

Example 1: Using Single Condition

The following example code filter Pandas DataFrame based on the column value using a single condition:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Henry', 'Lily'],
                       'Team':['A', 'B', 'C', 'A'],
                       'Age':[24, 19, 17, 15],
                       'Height':[4.3, 5.6, 6.3, 8.5]})
print(df, '\n')
print(df.loc[df['Height'] > 6.1])

In the above code:

  • The “pandas” library is imported.
  • The “DataFrame” is created with multiple columns using the “DataFrame()” function.
  • The “loc[]” method filters the Pandas DataFrame based on the specified column’s value using a particular condition.
  • According to the specified condition, only the value with a height greater than “1” will be returned in the output.

Output

pandas-filter-by-column-value-01.png

The input DataFrame has been filtered based on the column value “Height”.

Example 2: Using Multiple Condition

The below code filter DataFrame of Pandas using the specified column values:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Henry', 'Lily'],
                       'Team':['A', 'B', 'C', 'A'],
                       'Age':[24, 19, 17, 15],
                       'Height':[4.3, 5.6, 6.3, 5.5]})
print(df, '\n')
print(df.loc[(df['Age'] >= 17) & (df['Height'] < 5.5)])

Here in this code:

  • The “loc[]” method takes the multiple conditions based on the specified column value of DataFrame as an argument to filter the DataFrame.
  • According to the condition, the data having an age greater than “17” and a height smaller than “5.5” will be returned in the output.

Output

pandas-filter-by-column-value-02.png

The input DataFrame has been filtered by column value according to multiple conditions.

Method 2: Filter Pandas DataFrame by Column Value Using “Square Bracket”

The “Square Bracket” can also be used to filter Pandas DataFrame by column value. Here is an example:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Henry', 'Lily'],
                       'Team':['A', 'B', 'C', 'A'],
                       'Age':[24, 19, 17, 15],
                       'Height':[4.3, 5.6, 6.3, 5.5]})
print(df, '\n')
print(df[df['Age'] > 17])

In the above code:

  • The “df[df[‘Age’] > 17]” syntax is used to filter Pandas DataFrame based on the specified column value.
  • The condition indicates that only the rows with an “Age” column value greater than “17” will be returned.

Output

pandas-filter-by-column-value-03.png

The DataFrame has been filtered according to the column value.

Method 3: Filter Pandas DataFrame by Column Value Using “isin()” Method

The “df.isin()” method of Pandas DataFrame is used to verify if the particular value exists/present in the DataFrame. This can be utilized to filter DataFrame according to the particular column value. Here is an example:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Henry', 'Lily'],
                       'Team':['A', 'B', 'C', 'A'],
                       'Age':[24, 19, 17, 15],
                       'Height':[4.3, 5.6, 6.3, 8.5]})
print(df, '\n')
df1 = df[df['Team'].isin(['A', 'B'])]
print(df1)

In the above code:

  • The “isin()” method filters DataFrame by column value passed as an argument.
  • In this case, the “Team” column value “A” and “B” is passed to the “isin()” method to filter DataFrame.

Output

pandas-filter-by-column-value-04.png

The DataFrame has been filtered based on the “Team” column value.

Method 4: Filter Pandas DataFrame by Column Value Using “query()” Method

The “query()” method evaluates the Pandas DataFrame by taking the query string expression as an argument. Here we use this method to filter Pandas DataFrame according to the column value:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Henry', 'Lily'],
                       'Team':['A', 'B', 'C', 'A'],
                       'Age':[24, 19, 17, 15],
                       'Height':[4.3, 5.6, 6.3, 8.5]})
print(df, '\n')
df1=df.query("Age >= 19")
print(df1)

In this code block:

  • The “query()” method is used to filter Pandas DataFrame by taking the specified query expression “Age >= 19”.
  • This query expression indicates that DataFrame with an “Age” column value greater than or equal to “19” will be returned to output.

Output

pandas-filter-by-column-value-05.png

The DataFrame has been filtered by the specified column value.

Conclusion

The “df.loc[]”, “Square Bracket”, “isin()”, and “query()” methods are used to filter Pandas DataFrame based on the specified column value. These methods can be utilized to filter Pandas DataFrame by column value using single conditions or multiple conditions. This guide provided a tutorial on filtering Pandas DataFrame according to the particular column value.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...