This should be easy, assuming you have two columns. Use groupby
+ agg
. v1
should be aggregated by first
, and v2
should be aggregated by ','.join
.
df
key v1 v2
0 1 NaN a
1 2 NaN b
2 3 NaN c
3 2 NaN d
4 3 NaN e
(df.groupby('key')
.agg({'v1' : 'first', 'v2' : ','.join})
.reset_index()
.reindex(columns=df.columns))
key v1 v2
0 1 NaN a
1 2 NaN b,d
2 3 NaN c,e
If you have multiple such columns requiring the same aggregation, build an agg dict called f
and pass it to agg
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…