w3resource

Pandas DataFrame: Select rows from a given DataFrame based on values in some columns


24. Selecting Rows Based on Column Values

Write a Pandas program to select rows from a given DataFrame based on values in some columns.

Sample data:
Original DataFrame
col1 col2 col3
0 1 4 7
1 4 5 8
2 3 6 9
3 4 7 0
4 5 8 1
Rows for colum1 value == 4
col1 col2 col3
1 4 5 8
3 4 7 0

Sample Solution :

Python Code :

import pandas as pd
import numpy as np
d = {'col1': [1, 4, 3, 4, 5], 'col2': [4, 5, 6, 7, 8], 'col3': [7, 8, 9, 0, 1]}
df = pd.DataFrame(data=d)
print("Original DataFrame")
print(df)
print('Rows for colum1 value == 4')
print(df.loc[df['col1'] == 4])

Sample Output:

      Original DataFrame
   col1  col2  col3
0     1     4     7
1     4     5     8
2     3     6     9
3     4     7     0
4     5     8     1
Rows for colum1 value == 4
   col1  col2  col3
1     4     5     8
3     4     7     0            

Explanation:

The above code first creates a pandas DataFrame called ‘df’ with three columns 'col1', 'col2', and 'col3' and five rows of data.

df.loc[df['col1'] == 4]: This line selects only the rows of the DataFrame where the 'col1' column is equal to 4. This is done using a boolean mask created by the expression df['col1'] == 4. The .loc method is then used to select only the rows where the boolean mask is True.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to select rows where a specific column’s value equals a given number and then output only certain columns.
  • Write a Pandas program to filter rows based on a condition across multiple columns using the query() method.
  • Write a Pandas program to select rows using boolean indexing where one column's value exceeds another column's value.
  • Write a Pandas program to extract rows matching a pattern in a string column using regex.

Go to:


Previous: Write a Pandas program to rename columns of a given DataFrame.
Next: Write a Pandas program to change the order of a DataFrame columns.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.