2

Data.columns:

MultiIndex(levels=[['PDARS1M', 'PDARS1Y', 'PDKRW1M'], ['EB', 'EO', 'ER']],
       labels=[[2, 0, 1, 2, 0, 1, 2, 0, 1], [0, 0, 0, 2, 2, 2, 1, 1, 1]],
       names=['Instrument', 'Field'])

Data:

PDKRW1M PDARS1M PDARS1Y PDKRW1M PDARS1M PDARS1Y PDKRW1M PDARS1M PDARS1Y
EB  EB  EB  ER  ER  ER  EO  EO  EO
1125    40  53  1126    40  53  1127    40  54
1125    41  54  1126    41  54  1127    41  55
1126    41  54  1127    41  54  1128    41  55

I want to merge Data.columns.levels[0] and Data.columns.levels[1] and update as Data.columns

I used below script but got errors

Data.columns = Data.columns.levels[0] + '|' + Data.columns.levels[1]

"ValueError: Length mismatch: Expected axis has 9 elements, new values have 3 elements"

Expected Data Column Name:

PDKRW1M|EB, PDARS1M|EB, PDARS1Y|EB, PDKRW1M|ER, PDARS1M|ER, PDARS1Y|ER, PDKRW1M|EO, PDARS1M|EO, PDARS1Y|EO

1 Answer 1

2

Use map with join:

Data.columns = Data.columns.map('|'.join)

Or list comprehension:

Data.columns = [f'{a}{b}' for a, b in Data.columns]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.