Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select single row with DataFrame.loc[] without index

Assuming I have a DataFrame:

import pandas as pd
df = pd.DataFrame([["house", "red"], ["car", "blue"]], columns=["item", "colour"])

What is the idiomatic way to return a single row, or raise an exception if exactly one row is not found, using DataFrame.loc:

match = df.loc[df["colour"] == "red"]]
# I know there is exactly one row in the resulting DataFrame
# TODO: How to make match to return a single row as pd.Series

This would be similar to SQLAlchemy’s Query.one()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can squeeze and assert that the output is a Series (or use a test if you don’t want to raise an exception):

match = df.loc[df["colour"] == "red"].squeeze()

assert isinstance(match, pd.Series)

alternative to assert:

if isinstance(match, pd.Series):
    # do something
else:
    # do something else
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading