Q:

Expand/create pandas columns from grouped rows by concatenating subid value and other column names

I would like to create new columns from a panda dataframe by grouping based on a column and concatenating a subindex (in another column) with two other column names. This is best illustrated with an example. Say this is my input dataframe:
                  filename     sub_id    x    y
0  2019-07-29T16-01-33.jpg          0  731  343
1  2019-07-29T16-01-33.jpg          1  741  283
2  2019-07-29T16-01-34.jpg          0  734  407
3  2019-07-29T16-01-34.jpg          1  757  348
4  2019-07-29T16-01-35.jpg          0  741  293
5  2019-07-29T16-01-35.jpg          1  760  380

And I want to obtain this:
                  filename    x0   y0   x1   y1
0  2019-07-29T16-01-33.jpg   731  343  741  283
1  2019-07-29T16-01-34.jpg   734  407  757  348
2  2019-07-29T16-01-35.jpg   741  293  760  380

The sub_id value (0 or 1) is appended to the x and y column names to create new columns and the respective coordinate values transferred accordingly.
I'm assuming I have to use groupby in some way or joins, but not sure how.

A:

Yet another method:
# create the columns for x0, x1, y0, y1
df_unstacked= df.set_index(['filename', 'sub_id']).unstack(-1)
# rename the column
df_unstacked.columns= [''.join(map(str, c_tup)) for c_tup in df_unstacked.columns]

The result is
                          x0   x1   y0   y1
filename                                   
2019-07-29T16-01-33.jpg  731  741  343  283
2019-07-29T16-01-34.jpg  734  757  407  348
2019-07-29T16-01-35.jpg  741  760  293  380

