how to perform such pandas aggregation: drop nan and concatenate to the first?

Viewed 200

Input:

df = pd.DataFrame({
         'a':['1',np.nan,np.nan, '2',np.nan,np.nan],
         'b':['a',np.nan,'ddd',np.nan,'d','gg'],
         'c':[np.nan,'aa','bb',np.nan,'d',np.nan],

})
print (df)
     a    b    c
0    1    a  NaN
1  NaN  NaN   aa
2  NaN  ddd   bb
3    2  NaN  NaN
4  NaN    d    d
5  NaN   gg  NaN

Output:

   a      b      c
0  1  a ddd  aa bb
1  2   d gg      d
1 Answers

If there is non missing value for start of each group use ffill for forward filling missing values and aggregate all values with join and removed missing values:

df = df.groupby(df['a'].ffill()).agg(lambda x: ' '.join(x.dropna())).reset_index(drop=True)
print (df)
   a      b      c
0  1  a ddd  aa bb
1  2   d gg      d

Detail:

print (df['a'].ffill())
0    1
1    1
2    1
3    2
4    2
5    2
Name: a, dtype: object
Related