I have a dataframe with the following setup.
a foo b foo c foo Set
0 first second third fourth
The goal is to merge all columns with foo
in their header name and output the following:
All_foo Set
0 first fourth
1 second
2 third
I tried the following:
df2 = df.unstack().reset_index(drop=True).rename('All_foo').to_frame()
This gives me one single column with all the values merged. How can I make the unstacking process to be based on a condition so that I can get the output above?