Select row by column value in Pandas: Examples

Select row by column value in Pandas Examples

Do you want to select rows of the dataframe by column value? Then this post is for you. In this entire tutorial, you will know the different examples to implement the selection of rows in pandas based on the value of the column.

Select row by column value in Pandas Examples

Let’s create a sample dataframe that will be helpful for selecting rows by the column value. Run the below lines of code to create the sample dataframe.

import pandas as pd
data = {"Date":["12/11/2020","13/11/2020","14/11/2020","15/11/2020"],
"Open":[1,2,3,4],"Close":[5,6,7,8],"Volume":[100,200,300,400]}
df = pd.DataFrame(data=data)
print(df)

Output

Sample dataframe for selecting row by column value
Sample dataframe for selecting row-by-column value

Example 1: Select row by single column value in pandas

In this example, I will select rows on the basis of a single column value. For example, I want to get the rows of the dataframe where the volume is more than 200. To do so run the below line of code.

import pandas as pd
data = {"Date":["12/11/2020","13/11/2020","14/11/2020","15/11/2020"],
"Open":[1,2,3,4],"Close":[5,6,7,8],"Volume":[100,200,300,400]}
df = pd.DataFrame(data=data)
print(df.loc[df['Volume'] > 200])

Output

Select rows for the volume greater than 200
Select rows for a volume greater than 200

You can see all the rows that have a volume greater than 200 are selected.

In the same way, you can select rows of the dataframe by another column name in pandas.

Example 2: Select rows when multiple columns are satisfied

You can also select the rows on the value of more than one column. The rows will be selected when the condition for both columns are satisfied.

For example, I want to select rows that have a close price greater than 6 and volume are more than 300.

Run the below line of code to achieve it.

import pandas as pd
data = {"Date":["12/11/2020","13/11/2020","14/11/2020","15/11/2020"],
"Open":[1,2,3,4],"Close":[5,6,7,8],"Volume":[100,200,300,400]}
df = pd.DataFrame(data=data)
print(df.loc[(df['Close'] > 6) & (df['Volume'] > 300)])

Output

Select rows when the both columns conditon are satisfied
Select rows when the both columns conditon are satisfied

Example 3: Select rows when one of the  column conditions is satisfied

In the above example, you were selecting rows where conditions for more than one column are satisfied. But in this example rows will be selected when the condition of one of the columns is satisfied.

For example Instead of & in the above example if I will use the or ( | ) then you will get more the one row.

Run the below lines of code and see the output.

import pandas as pd
data = {"Date":["12/11/2020","13/11/2020","14/11/2020","15/11/2020"],
"Open":[1,2,3,4],"Close":[5,6,7,8],"Volume":[100,200,300,400]}
df = pd.DataFrame(data=data)
print(df.loc[(df['Close'] > 6) | (df['Volume'] > 300)])

Output

Select rows when the one of columns conditon are satisfied
Select rows when the one of columns conditon are satisfied

Conclusion

Sometimes you may face difficulty in selecting rows by the column value if you don’t know how to use the conditions. In this post, you have learned through the examples how to select rows by single column value in pandas.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner