Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multi-index tuple join

I have the following column of a multiple index tuple in this form:

df_incumbent_Q.columns

Which looks like this:

MultiIndex([('Cust Linehaul', '2022Q2'),
            ('Cust Linehaul', '2022Q3'),
            ('Cust Linehaul', '2022Q4'),
            (  'Load Number', '2022Q2'),
            (  'Load Number', '2022Q3'),
            (  'Load Number', '2022Q4')],
           names=[None, 'Quarter'])

I am trying to combine the columns name so that I have something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

           ['Cust Linehaul_2022Q2',
            'Cust Linehaul_2022Q3',
            'Cust Linehaul_2022Q4',
              'Load Number_2022Q2',
              'Load Number_2022Q3',
              'Load Number_2022Q4']

I tried to join them this way

df_incumbent_Q.columns =[''.join(str(c)) for c in df_incumbent_Q.columns]

but it comes back with Period aggregation.

Index(['('Cust Linehaul', Period('2022Q2', 'Q-DEC'))',
   '('Cust Linehaul', Period('2022Q3', 'Q-DEC'))',
   '('Cust Linehaul', Period('2022Q4', 'Q-DEC'))',
   '('Load Number', Period('2022Q2', 'Q-DEC'))',
   '('Load Number', Period('2022Q3', 'Q-DEC'))',
   '('Load Number', Period('2022Q4', 'Q-DEC'))'],
  dtype='object')

Then I tried my luck with str.replace method, but can’t get rid of the Period( ) parenthesis.

pattern = '|'.join(["Period","'Q-DEC'",''])
df_incumbent_Q.columns.str.replace(pattern,'',regex=True)

The output is like this

Index(['('Cust Linehaul', ('2022Q2', ))', '('Cust Linehaul', ('2022Q3', ))',
   '('Cust Linehaul', ('2022Q4', ))', '('Load Number', ('2022Q2', ))',
   '('Load Number', ('2022Q3', ))', '('Load Number', ('2022Q4', ))'],
  dtype='object')

Ideally I want to solve it through .join() method if that doesn’t work I want to solve it through .replace method.

>Solution :

Try:

df_incumbent_Q.columns =[f'{a}_{b}' for a, b in df_incumbent_Q.columns]

Prints:

   Cust Linehaul_2022Q2  Cust Linehaul_2022Q3  Cust Linehaul_2022Q4  Load Number_2022Q2  Load Number_2022Q3  Load Number_2022Q4
0                   NaN                   NaN                   NaN                 NaN                 NaN                 NaN
1                   NaN                   NaN                   NaN                 NaN                 NaN                 NaN

Input dataframe:

        Cust Linehaul               Load Number              
Quarter        2022Q2 2022Q3 2022Q4      2022Q2 2022Q3 2022Q4
0                 NaN    NaN    NaN         NaN    NaN    NaN
1                 NaN    NaN    NaN         NaN    NaN    NaN
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading