Select Rows From List of Values in Pandas DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:October 30, 2023

You can select rows from a list of values in pandas DataFrame either using DataFrame.isin(), DataFrame.query(), DataFrame.index(), DataFrame.loc[] attribute or DataFrame.apply() method with a lambda function. In this article, I will explain how to use a list of values to select rows from pandas DataFrame with Examples.

Related:

1. Quick Examples of Select DataFrame Rows From List of Values in Pandas

If you are in a hurry, below are some quick examples of how to use a list of values to select rows from pandas DataFrame.


# Below are quick examples.

# Create a list of values for select rows using isin([]) method
list_of_values = [25000, 30000]
df2 = df[df['Fee'].isin(list_of_values)]

# Using isin([]) method to get opposite rows
df2 = df[~df['Fee'].isin([25000, 30000])]

# Using DataFrame.query() method to select rows
df2 = df.query('Discount in [1000,2000]')

# Filter Rows by list of values
df2 = df.query('Discount == [1000,2000]')

# Select list of rows using variable
list_of_value = [1000,2000]
df2 = df.query('Discount in @list_of_value')

# Using DataFrame.query() method
list_of_value = [1000,2000]
df2 = df.query('Discount == @list_of_value')

# Select rows using DataFrame.loc[] to apply() and lambda function 
df2 = df.loc[df.apply(lambda x: x.Fee in [20000,22000], axis=1)]

# Select pandas rows based on list index
index_list = [0,2]
df2 = df.loc[df.index[index_list]]

# Using DataFrame.iloc[] to select rows list index
index_list = [0,2]
df2 = df.iloc[index_list]

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      1200
r4   pandas  30000   50days      2000

2. Using DataFrame.isin() to Select Rows From List of Values

DataFrame.isin() method is used to filter/select rows from a list of values. You can have the list of values in variable and use it on isin() or use it directly. Let’s see these examples.


# Create a list of values for select rows using isin([]) method
list_of_values = [25000, 30000]
df2 = df[df['Fee'].isin(list_of_values)]
print(df2)

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r2  PySpark  25000   40days      2300
r4   pandas  30000   50days      2000

And to select the rows that are not in the list of values using ~ .


# Using isin([]) method to get opposite rows
df2 = df[~df['Fee'].isin([25000, 30000])]
print(df2)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
r1   Spark  20000   30days      1000
r3  Python  22000   35days      1200

3. Using DataFrame.query() Method to Select Multiple Rows Values

Also, use pandas.DataFrame.query() to filter Pandas DataFrame rows from a list of multiple values. With this, you can use in and == operators.


# Using DataFrame.query() method to select rows
df2 = df.query('Discount in [1000,2000]')
print(df2)

# Filter Rows by list of values
df2 = df.query('Discount == [1000,2000]')
print(df2)

# Select list of rows using variable
list_of_value = [1000,2000]
df2 = df.query('Discount in @list_of_value')
print(df2)

# Using DataFrame.query() method
list_of_value = [1000,2000]
df2 = df.query('Discount == @list_of_value')
print(df2)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
r1   Spark  20000   30days      1000
r4  pandas  30000   50days      2000

4. Select Rows Using DataFrame.loc[] with apply() and Lambda

Use DataFrame.loc[] along with apply() and lambda function to select rows that are matching in a list.


# Select rows using DataFrame.loc[] to apply() and lambda function 
df2 = df.loc[df.apply(lambda x: x.Fee in [20000,22000], axis=1)]
print(df2)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
r1   Spark  20000   30days      1000
r3  Python  22000   35days      1200

5. Select Pandas Rows Based on List Index

You can use loc[] or iloc[] with your desired indices to select rows from DataFrame. For more details refer to Using Index with loc[] to select rows from DataFrame.


# Select pandas rows based on list index
index_list = [0,2]
df2 = df.loc[df.index[index_list]]
print(df2)

# Using DataFrame.iloc[] to select rows list index
index_list = [0,2]
df2 = df.iloc[index_list]
print(df2)

Yields below output.


# Output:
  Courses    Fee Duration  Discount
0   Spark  20000   30days      1000
2  Python  22000   35days      1200

6. Complete Example to Select Rows From List of Values in DataFrame


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
df2 = df[df['Duration'].str.contains('35days')]
print(df2)

# Create a list of values for select rows using isin([]) method
list_of_values = [25000, 30000]
df2 = df[df['Fee'].isin(list_of_values)]
print(df2)

# Using isin([]) method to get opposite rows
df2 = df[~df['Fee'].isin([25000, 30000])]
print(df2)

# Using DataFrame.query() method to select rows
df2 = df.query('Discount in [1000,2000]')
print(df2)

# Filter Rows by list of values
df2 = df.query('Discount == [1000,2000]')
print(df2)

# Select list of rows using variable
list_of_value = [1000,2000]
df2 = df.query('Discount in @list_of_value')
print(df2)

# Using DataFrame.query() method
list_of_value = [1000,2000]
df2 = df.query('Discount == @list_of_value')
print(df2)

# Select rows using DataFrame.loc[] to apply() and lambda function 
df2 = df.loc[df.apply(lambda x: x.Fee in [20000,22000], axis=1)]
print(df2)

# Select pandas rows based on list index
index_list = [0,2]
df2 = df.loc[df.index[index_list]]
print(df2)

# Using DataFrame.iloc[] to select rows list index
index_list = [0,2]
df2 = df.iloc[index_list]
print(df2)

Conclusion

In this article, you have learned how to use a list of values to select rows from DataFrame using DataFrame.isin(), DataFrame.query() and DataFrame.index() functions. Also, you have learned to select rows with row indices.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply

You are currently viewing Select Rows From List of Values in Pandas DataFrame