Explore Courses Blog Tutorials Interview Questions
+3 votes
in Python by (3.9k points)
edited by

I want to select rows from a DataFrame based on values in some column in pandas, How can I do it?

I use this in SQL:

FROM table
WHERE column_name = some_value

2 Answers

0 votes
by (2k points)
edited by

There are many methods to do it  but personally I'll suggest you to use mask, for instance check this example:

from pandas import DataFrame

# Create data set
q = {'Revenue':[200,112,221], 
df = DataFrame(q)

# mask = Return True when the value in column "Revenue" is equal to 111
mask = df['Revenue'] == 112

print mask

# Result:
# 0    False
# 1     True
# 2    False
# Name: Revenue, dtype: bool

# Select * FROM df WHERE Revenue = 112

# Result:
#    Cost    Revenue
# 1  441     112
0 votes
by (106k points)
edited by

You can use the below-mentioned code:-

import pandas as pd

d = {'foo':[100, 111, 222],'bar':[333, 444, 555]}

df = pd.DataFrame(d)


df[ == 222]

You can use the following video tutorials to clear all your doubts:-

Be a Python Expert. Enroll in Python Programming Course by Intellipaat.

Browse Categories