Comparison with other methods

Example 1

In this example, we are using a mixture of NumPy and pandas method

Python3




# to calculate timing
import numpy as np
% % timeit
 
 
# using mixture of numpy and pandas method
df_new = df.iloc[np.where(df.name.isin(li))]


Output:

756 µs ± 132 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Example 2

In this example, we are using only the Pandas method

Python3




# to calculate time
%%timeit
 
li=['Albert','Louis','John']
 
# Pandas method only
df[(df.points>50)&(~df.name.isin(li))]


Output

1.7 ms ± 307 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)



How to select rows from a dataframe based on column values ?

In this article, we will cover how we select rows from a DataFrame based on column values in Python

The rows of a Dataframe can be selected based on conditions as we do use the SQL queries. The various methods to achieve this is explained in this article with examples. 

Similar Reads

Importing Dataset for demonstration

To explain the method a dataset has been created which contains data of points scored by 10 people in various games. The dataset is loaded into the Dataframe and visualized first. Ten people with unique player id(Pid) have played different games with different game id(game_id) and the points scored in each game are added as an entry to the table. Some of the player’s points are not recorded and thus NaN value appears in the table....

Method 1: Boolean Indexing method

...

Method 2: Positional indexing method

In this method, for a specified column condition, each row is checked for true/false. The rows which yield True will be considered for the output. This can be achieved in various ways. The query used is Select rows where the column Pid=’p01′...

Method 3: Using dataframe.query() method

...

Method 3: Using isin() method

...

Method 4: Using Numpy.where() method

...

Method 5: Comparison with other methods

The methods loc() and iloc() can be used for slicing the Dataframes in Python. Among the differences between loc() and iloc(), the important thing to be noted is iloc() takes only integer indices, while loc() can take up boolean indices also....