I have a huge dataset arranged like this
Serial Val1 Val2 Val3
1 21.10
1 43.06
1 32.12
2 11.20
2 22.20
3 45.10
3 14.16
4 34.90
4 12.12
4 18.09
I would like to groupby each unique serial and consolidate its corresponding values (from Val1 to Val3) to one column ['All'] and also place a ['Source'] column.
Serial Val1 Val2 Val3 All Source
1 21.10 21.10 Val1
1 43.06 43.06
1 32.12 32.12
2 11.20 11.20 Val2
2 22.20 22.20
3 45.10 45.10 Val1
3 14.16 14.16
4 34.90 34.90 Val3
4 12.12 12.12
4 18.09 18.09
I tried doing something like this,
df['All'] = df['Serial'].map(df.groupby('Serial').apply(lambda x: x['Val2'] if pd.isnull(x['Val1']) else x['Val3'])