Pandas Loc Multiple Conditions

Select rows by multiple conditions using loc in Pandas

💡 Outline

Here is the code to select rows by pandas Loc multiple conditions.

Here, we are select rows of DataFrame where age is greater than 18 and name is equal to Jay.

The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels.

We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a condition with the DataFrame.

See the code below.

Output:

Note that this returns a Series object with True or False values depending on the rows. We can use this object to extract the rows which satisfy the condition.

For example,

Output:

In the above example, we extracted the rows from a DataFrame where age was greater than 18. Similarly, we can specify multiple conditions.

While giving multiple conditions, remember that we need to separate the conditions using the relational operators.

  • We can use the OR operator when we want to print the rows if at least even one condition is True.
  • The AND operator is used when we wish to return rows where both the conditions are True.

We can use the loc() function also to extract rows based on some condition. We will repeat what we did in the previous example using the loc() function.

See the code below.

Output:

This method is a lot cleaner. Now we will specify multiple conditions within the loc() function.

For example,

Output:

In the above example, we extracted the rows from the DataFrame, where the Name was equal to Jay and Age was greater than 18. Both the conditions were separated using the & operator. This required both the conditions to be true in any given row.

That’s all about how to Select rows with Pandas loc multiple conditions.

Was this post helpful?

Related Posts

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() functionWas this post helpful? We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over […]

  • Find rows with nan in Pandas
    11 January

    Find rows with nan in Pandas

    Table of ContentsWhat is nan values in Pandas?Find rows with NAN in pandasFind columns with nan in pandasFind rows with nan in Pandas using isna and iloc()Was this post helpful? In this post, we will see how to find rows with nan in Pandas. What is nan values in Pandas? A pandas DataFrame can contain […]

  • 05 October

    Pandas replace values in column

    Table of ContentsUsing the loc() function to replace values in column of pandas DataFrameUsing the iloc() function to to replace values in column of pandas DataFrameUsing the map() function to replace values of a column in a pandas DataFrameUsing the replace() function to replace values in column of pandas DataFrameUsing the where() function to replace […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in PythonWas this post helpful? In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in PandasWas this post helpful? A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from […]

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.