Python pandas unique value ignoring NaN
This will work for what you need:
df.fillna(method='ffill').groupby('b').agg({'a': ['min', 'max', 'unique'], 'c': ['first', 'last', 'unique']})
Because you use min
, max
and unique
repeated values do not concern you.
Update 23 November 2020
This answer is terrible, don't use this. Please refer @IanS's answer.
Earlier
Try ffill
df.ffill().groupby('b').agg({'a': ['min', 'max', 'unique'], 'c': ['first', 'last', 'unique']})
c a first last unique min max unique b 0 foo foo [foo] 1.0 2.0 [1.0, 2.0] 1 bar bar [bar, foo, baz] 1.0 3.0 [1.0, 3.0]
If Nan is the first element of the group then the above solution breaks.
Define a function:
def unique_non_null(s):
return s.dropna().unique()
Then use it in the aggregation:
df.groupby('b').agg({
'a': ['min', 'max', unique_non_null],
'c': ['first', 'last', unique_non_null]
})