How can I use the “AND” operator in Pandas to combine multiple conditions?

How can I use the “AND” operator in Pandas to combine multiple conditions?

The “AND” operator in Pandas is a logical operation that allows for the combination of multiple conditions in a data frame. This operator follows the boolean logic of returning a “True” value only when all conditions are satisfied. It can be used in various ways, such as filtering data frames or creating new columns based on specific criteria. By using the “AND” operator, users can efficiently manipulate and analyze data in their Pandas data frames.

Use “AND” Operator in Pandas (With Examples)


You can use the & symbol as an “AND” operator in pandas.

For example, you can use the following basic syntax to filter for rows in a pandas DataFrame that satisfy condition 1 and condition 2:

df[(condition1) & (condition2)]

The following examples show how to use this “AND” operator in different scenarios.

Example 1: Use “AND” Operator to Filter Rows Based on Numeric Values in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'B', 'B', 'C', 'C'],
                   'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      25        5        11
1    A      12        7         8
2    B      15        7        10
3    B      14        9         6
4    B      19       12         6
5    B      23        9         5
6    C      25        9         9
7    C      29        4        12

We can use the following syntax to filter for rows in the DataFrame where the value in the points column is greater than 20 and the value in the assists column is equal to 9:

#filter rows where points > 20 and assists = 9df[(df.points > 20) & (df.assists == 9)]

        team	points	assists	rebounds
5	B	23	9	5
6	C	25	9	9

The only rows returned are the ones where the points value is greater than 20 and the assists value is equal to 9.

Example 2: Use “AND” Operator to Filter Rows Based on String Values in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'position': ['G', 'G', 'F', 'F', 'C', 'F', 'C', 'C'],
                   'conference': ['W', 'W', 'W', 'W', 'E', 'E', 'E', 'E'],
                   'points': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team position conference  points
0    A        G          W      11
1    B        G          W       8
2    C        F          W      10
3    D        F          W       6
4    E        C          E       6
5    F        F          E       5
6    G        C          E       9
7    H        C          E      12

We can use the following syntax to filter for rows in the DataFrame where the value in the position column is equal to G and the value in the conference column is equal to W:

#filter rows based on string valuesdf[(df.position == 'G') & (df.conference == 'W')]

team	positionconferencepoints
0	A	G	W	11
1	B	G	W	8

The only rows returned are the ones where the position column is equal to G and the conference column is equal to W.

Additional Resources

Cite this article

stats writer (2024). How can I use the “AND” operator in Pandas to combine multiple conditions?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-and-operator-in-pandas-to-combine-multiple-conditions/

stats writer. "How can I use the “AND” operator in Pandas to combine multiple conditions?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-and-operator-in-pandas-to-combine-multiple-conditions/.

stats writer. "How can I use the “AND” operator in Pandas to combine multiple conditions?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-and-operator-in-pandas-to-combine-multiple-conditions/.

stats writer (2024) 'How can I use the “AND” operator in Pandas to combine multiple conditions?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-and-operator-in-pandas-to-combine-multiple-conditions/.

[1] stats writer, "How can I use the “AND” operator in Pandas to combine multiple conditions?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use the “AND” operator in Pandas to combine multiple conditions?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top