Home > Enterprise >  Transform a series into a multiindex, with the same backward name
Transform a series into a multiindex, with the same backward name

Time:08-02

So i have the following series

s = pd.Series({'affiliate': 120,
 'attendance bot': 7422,
 'botao-contrate': 953,
 'carrinho abandonado e-commerce/sms': 31,
 'carrinho-abandonado-ecommerce-email': 4574,
 'carrinho-abandonado-ecommerce-sms': 14332,
 'click-to-whatsapp': 1,
 'midias': 2,
 'reschedule': 808,
 'site': 11673,
 'undefined-origin': 5914})

And the following multiindex df

df = pd.DataFrame({('1-Onboarding   Retorno', 'affiliate'): 118,
 ('1-Onboarding   Retorno', 'attendance bot'): 7298,
 ('1-Onboarding   Retorno', 'botao-contrate'): 935,
 ('1-Onboarding   Retorno', 'carrinho abandonado e-commerce/sms'): 31,
 ('1-Onboarding   Retorno', 'carrinho-abandonado-ecommerce-email'): 4165,
 ('1-Onboarding   Retorno', 'carrinho-abandonado-ecommerce-sms'): 13888,
 ('1-Onboarding   Retorno', 'click-to-whatsapp'): 1,
 ('1-Onboarding   Retorno', 'midias'): 2,
 ('1-Onboarding   Retorno', 'reschedule'): 775})

As you can see the index on the level 1 of the indexed dataframe match, how could i concatenate the df and the series, stipulating a new multiindex to the series

Wanted result:

pd.DataFrame({('0 - New index of the series','affiliate'):120,
     ('0 - New index of the series','attendance'):7422, # And so on
     ('1-Onboarding   Retorno', 'affiliate'): 118,
     ('1-Onboarding   Retorno', 'attendance bot'): 7298,
     ('1-Onboarding   Retorno', 'botao-contrate'): 935,
     ('1-Onboarding   Retorno', 'carrinho abandonado e-commerce/sms'): 31,
     ('1-Onboarding   Retorno', 'carrinho-abandonado-ecommerce-email'): 4165,
     ('1-Onboarding   Retorno', 'carrinho-abandonado-ecommerce-sms'): 13888,
     ('1-Onboarding   Retorno', 'click-to-whatsapp'): 1,
     ('1-Onboarding   Retorno', 'midias'): 2,
     ('1-Onboarding   Retorno', 'reschedule'): 775})

Sorry for the dump of data but it was the best i could do

CodePudding user response:

You can create multiindex columns dataframe from s and concat them

cols = pd.MultiIndex.from_product([['0 - New index of the series'], s.index.tolist()])
df1 = pd.DataFrame([s.values], columns=cols)
out = pd.concat([df1, df], axis=1)
print(df1)

  0 - New index of the series                                \
                    affiliate attendance bot botao-contrate
0                         120           7422            953

                                                                          \
  carrinho abandonado e-commerce/sms carrinho-abandonado-ecommerce-email
0                                 31                                4574

                                                                         \
  carrinho-abandonado-ecommerce-sms click-to-whatsapp midias reschedule
0                             14332                 1      2        808


    site undefined-origin
0  11673             5914


print(out)

  0 - New index of the series                                \
                    affiliate attendance bot botao-contrate
0                         120           7422            953

                                                                          \
  carrinho abandonado e-commerce/sms carrinho-abandonado-ecommerce-email
0                                 31                                4574

                                                                         \
  carrinho-abandonado-ecommerce-sms click-to-whatsapp midias reschedule
0                             14332                 1      2        808

                          1-Onboarding   Retorno                 \
    site undefined-origin              affiliate attendance bot
0  11673             5914                    118           7298

                                                     \
  botao-contrate carrinho abandonado e-commerce/sms
0            935                                 31

                                                                         \
  carrinho-abandonado-ecommerce-email carrinho-abandonado-ecommerce-sms
0                                4165                             13888


  click-to-whatsapp midias reschedule
0                 1      2        775
  • Related