简体   繁体   中英

Check if Pandas Groupby is empty

I have a usecase, where I m filtering on a row_key which is being supplied as an argument

I m aware of the standard empty property to check for a DataFrame , however could not find a similar one for DataFrameGroupBy

Below is a code snippet that demonstrates the usecase, the data supplied is dummy -

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO("""
Name,Value,row_key,row_label
XYZ,100,abc,"Label - 2"
ASD,100,abc,"Label - 2"
GHJ,1000,abc,"Label - 2"
KLI,100,abc,"Label - 2"
BHY,1009,bnm,"Label - 2"
TGB,1409,bnm,"Label - 2"
YUJ,1509,bnm,"Label - 2"
KUT,1609,bnm,"Label - 2"
"""))

invalid_row_key = 'fgh'

filter_df = df[df['row_key'] == invalid_row_key].groupby('row_label')

#### I want something similar to below if case , to handle if the filter_df is empty
if filter_df.empty:
   print("No Row Key Present")

I m aware I can use recent_index on the grouped-dataframed, but I wanted to check if there is a better way of handling this

Query the .ngroups attribute, at least 1 group should be present for >= 1 rows:

if not filter_df.ngroups:
    # no row key present

See this post by me for more on ngroups .


The answer above assumes you cannot tell in advance of creating a groupBy object, but if you can, it is better to check at the condition step, then you can check the DataFrame.empty attribute instead which is more intuitive:

filter_df = df[df['row_key'] == invalid_row_key]
if filter_df.empty:
    # no rows present
else:
    filter_df.groupby('row_label').doSomethingElse()

The best way to handle this would to filter the dataframe before the grouping condition as stated by other answers.

However I tried the following solution myself.


if filter_df.size().empty:
   print("Empty DataFrame")
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM