Reputation: 1219
I have a df like this.
>>> df
c1 t1 c2
0 NaN 20 xyz
1 NaN 20 x1y1z1
2 NaN 20 NaN
>>> df.groupby(['t1'], as_index=False).agg(lambda x: ';'.join(x.astype(str)))
t1 c1 c2
0 20 nan;nan;nan xyz;x1y1z1;nan
But I don't want to join columns values if it's np.nan
desired o/p
t1 c1 c2
0 20 NaN xyz;x1y1z1
How this check can be done here?
Thanks
Upvotes: 1
Views: 92
Reputation: 153500
You could try using dropna
:
df.groupby('t1', as_index=False)\
.agg(lambda x: ';'.join(x.dropna().astype(str)))\
.replace('',np.nan)
Output:
t1 c1 c2
0 20 NaN xyz;x1y1z1
Upvotes: 4