+3 votes
2 views
in Python by (1.3k 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

1 Answer

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
...