简体   繁体   中英

pandas groupby for non missing values

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

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

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