william007
william007

Reputation: 18545

groupby agg with first non-null unique value

Following code gives error

import pandas as pd
import numpy as np
df=pd.DataFrame({"item":['a','a','b'],"item1":['b','d','c']})

df.groupby("item").agg(model_list=("item1", np.unique))

Since there are two unique values for item a (i.e., b and d), how to modify it to return the first non-null unique value?

Upvotes: 2

Views: 889

Answers (1)

jezrael
jezrael

Reputation: 863166

Use GroupBy.first which by default remove missing values, so returned first non missing value:

df=pd.DataFrame({"item":['a','a','b','b','b'],"item1":['b','d',np.nan, np.nan, 'c']})
        

df = df.groupby("item").agg(model_list=("item1", 'first'))
print (df)
     model_list
item           
a             b
b             c

Upvotes: 2

Related Questions