I am sorry for asking but I did not get the still existing answers. I simply glued two data frames with the same column names.
| | X | Y | X | Y |
|---:|----:|----:|----:|----:|
| 0 | 1 | 3 | 9 | 7 |
| 1 | 2 | 4 | 8 | 6 |
What I want is
| | FOO | BAR |
| | X | Y | X | Y |
|---:|----:|----:|----:|----:|
| 0 | 1 | 3 | 9 | 7 |
| 1 | 2 | 4 | 8 | 6 |
I tried pd.MultiIndex.from_product([c.columns, ['FOO', 'BAR']]) but this results in
MultiIndex([('X', 'FOO'),
('X', 'BAR'),
('Y', 'FOO'),
('Y', 'BAR'),
('X', 'FOO'),
('X', 'BAR'),
('Y', 'FOO'),
('Y', 'BAR')],
)
But I need
MultiIndex([('X', 'FOO'),
('Y', 'FOO'),
('X', 'BAR'),
('Y', 'BAR')],
)
This is an MWE
#!/usr/bin/env python3
import pandas as pd
a = pd.DataFrame({'X': [1,2], 'Y': [3, 4]})
b = pd.DataFrame({'X': [9,8], 'Y': [7, 6]})
c = pd.concat([a, b], axis=1)
# throws a ValueError: Length mismatch: Expected axis has 4 elements, new values have 8 elements
c.columns = pd.MultiIndex.from_product([c.columns, ['FOO', 'BAR']])
Would it help to do something to the two separate DataFrames before I concat() them?
pd.MultiIndex.from_product([df.columns.unique(), ['FOO', 'BAR']])?c.columns = pd.MultiIndex.from_arrays((pd.Index(['FOO', 'BAR']).repeat(2), c.columns))