search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
chevron_left Selecting Data Cookbook
Accessing a single value of a DataFrameAccessing columns of a DataFrame using column labelsAccessing columns of a DataFrame using integer indicesAccessing rows of a DataFrame using integer indicesAccessing rows of a DataFrame using row labelsAccessing the first n rowsAccessing the last n rowsAccessing values of a multi-index DataFrameAdding prefix to column labelsAdding suffix to column labelsConverting two columns into a dictionaryExcluding columns based on typeGetting earliest or latest date from DataFrameGetting every nth rowGetting indexes of rows matching conditionsGetting shortest and longest stringsSelecting a single column as a DataFrameSelecting columns of a DataFrame using regexSelecting last column of DataFrameSelecting rows based on a conditionExtracting values of a DataFrame as a Numpy arrayGetting a list of all the column labelsGetting all columns except oneGetting all duplicate rowsGetting all numeric columns of a DataFrameGetting all unique values of columnsGetting column label of max value in each rowGetting column label of minimum value in each rowGetting columns by data typeGetting columns using integer indexGetting first row value of a columnGetting index of Series where value is TrueGetting integer index of a column using its column labelGetting integer index of rows based on column valuesGetting multiple columnsGetting row with largest index valueGetting row with smallest index valueGetting rows based on multiple column valuesGetting rows except someGetting rows from a DataFrame based on column valuesGetting rows that are not in other DataFrameGetting rows using OR statementGetting rows where column values are of specific lengthGetting rows where value is between two valuesGetting rows where values do not contain substringGetting the column labels of a DataFrameGetting the first columnGetting the index of a DataFrameGetting the length of the longest string in a columnGetting the longest string in a columnGetting the row with the maximum column valueGetting the row with the minimum column valueGetting the shape of a DataFrameGetting number of columns of a DataFrameGetting the total number of rows of a DataFrameGetting the total number of values in a DataFrameMaking column labels all lowercaseMaking column labels all uppercaseRandomly select rows based on a conditionRandomly selecting n columns from a DataFrameRandomly selecting n rows from a DataFrameReassigning column valuesRetrieving DataFrame column values as a NumPy arraySelecting columns that do not begin with certain prefixSelecting columns with certain prefixSelecting n rows with the smallest values for a columnSelecting rows based on datesSelecting rows from a DataFrame whose column values are contained in a listSelecting rows from a DataFrame whose column values are NOT contained in a listSelecting rows from a DataFrame whose column values contain a substringSelecting top n rows with the largest values for a columnSplitting DataFrame based on column values
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook
chevron_left Selecting Data Cookbook
Accessing a single value of a DataFrameAccessing columns of a DataFrame using column labelsAccessing columns of a DataFrame using integer indicesAccessing rows of a DataFrame using integer indicesAccessing rows of a DataFrame using row labelsAccessing the first n rowsAccessing the last n rowsAccessing values of a multi-index DataFrameAdding prefix to column labelsAdding suffix to column labelsConverting two columns into a dictionaryExcluding columns based on typeGetting earliest or latest date from DataFrameGetting every nth rowGetting indexes of rows matching conditionsGetting shortest and longest stringsSelecting a single column as a DataFrameSelecting columns of a DataFrame using regexSelecting last column of DataFrameSelecting rows based on a conditionExtracting values of a DataFrame as a Numpy arrayGetting a list of all the column labelsGetting all columns except oneGetting all duplicate rowsGetting all numeric columns of a DataFrameGetting all unique values of columnsGetting column label of max value in each rowGetting column label of minimum value in each rowGetting columns by data typeGetting columns using integer indexGetting first row value of a columnGetting index of Series where value is TrueGetting integer index of a column using its column labelGetting integer index of rows based on column valuesGetting multiple columnsGetting row with largest index valueGetting row with smallest index valueGetting rows based on multiple column valuesGetting rows except someGetting rows from a DataFrame based on column valuesGetting rows that are not in other DataFrameGetting rows using OR statementGetting rows where column values are of specific lengthGetting rows where value is between two valuesGetting rows where values do not contain substringGetting the column labels of a DataFrameGetting the first columnGetting the index of a DataFrameGetting the length of the longest string in a columnGetting the longest string in a columnGetting the row with the maximum column valueGetting the row with the minimum column valueGetting the shape of a DataFrameGetting number of columns of a DataFrameGetting the total number of rows of a DataFrameGetting the total number of values in a DataFrameMaking column labels all lowercaseMaking column labels all uppercaseRandomly select rows based on a conditionRandomly selecting n columns from a DataFrameRandomly selecting n rows from a DataFrameReassigning column valuesRetrieving DataFrame column values as a NumPy arraySelecting columns that do not begin with certain prefixSelecting columns with certain prefixSelecting n rows with the smallest values for a columnSelecting rows based on datesSelecting rows from a DataFrame whose column values are contained in a listSelecting rows from a DataFrame whose column values are NOT contained in a listSelecting rows from a DataFrame whose column values contain a substringSelecting top n rows with the largest values for a columnSplitting DataFrame based on column values
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

Getting rows from a DataFrame based on column values in Pandas

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Selecting Data Cookbook
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

To get rows from a DataFrame based on column values, use the DataFrame's query(~) method.

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,4,5], "B":[6,7,8]}, index=["a","b","c"])
df
A B
a 3 6
b 4 7
c 5 8

Equality

To get all rows where the value for column A equals 4:

df.query("A == 4")
A B
b 4 7

For a not-equals query, use != instead

Note that if the value is of type string, then wrap the value in ' or " like so:

df = pd.DataFrame({"A":["a","b"]})
df.query("A == 'a'")
A
0 a

Inequality

To get all rows where the value for column A is greater than 3:

df.query("A > 3")
A B
b 4 7
c 5 8

AND queries

To get all rows where A > 3 and B < 8:

df.query("A > 3 & B < 8")
A B
b 4 7

Here, you could use "and" instead of "&" like so:

df.query("A > 3 and < 8")
A B
b 4 7

OR queries

To get all rows where A == 3 or B == 8:

df.query("A == 3 | B == 7")
A B
a 3 6
b 4 7

Here, you could use "or" instead of "|" like so:

df.query("A == 3 or B == 7")
A B
a 3 6
b 4 7

For your reference, we show the df again:

df
A B
a 3 6
b 4 7
c 5 8

Queries with column-comparison

To get all rows where the value for column B is greater than that for column A:

df.query("A < B")
A B
a 3 6
b 4 7
c 5 8

Queries with arithmetic operations

To get all rows where value for column B equals is based on some function of A:

df.query("B == (A*2)-2")
A B
c 5 8
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!