Skip to content Skip to sidebar Skip to footer

With Pandas In Python, Select Only The Rows Where Group By Group Count Is 1

I've filtered my data as suggested here: With Pandas in Python, select the highest value row for each group author cat val 0 author1 category2 15 1 author2 catego

Solution 1:

Easier

df.groupby('author').filter(lambda x: len(x)==1)


     author        cat  val
id0   author1  category2   151   author2  category4    9

Solution 2:

my solution is a bit more complex but still working

def groupbyOneOccurrence(df):
    grouped = df.groupby("author")
    retDf = pd.DataFrame()
    forgroupin grouped:
        iflen(group[1]._get_values) == 1:
            retDf = pd.concat([retDf, group[1]])
    return retDf


author        cat val
0  author1  category2  151  author2  category4   9

Post a Comment for "With Pandas In Python, Select Only The Rows Where Group By Group Count Is 1"