Home > Software engineering >  Select single row with DataFrame.loc[] without index
Select single row with DataFrame.loc[] without index

Time:08-12

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()

CodePudding user response:

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
  • Related