Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
5 views
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:

SELECT *
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], 
     'Cost':[331,441,551]}
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
df[mask]

# 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

df[df.foo == 222]

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

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

31k questions

32.4k answers

500 comments

693 users

Browse Categories

...