Home > database >  Is there a way to re-order duplicate index so that they appear cumulatively?
Is there a way to re-order duplicate index so that they appear cumulatively?

Time:12-30

I have duplicated a dataframe using the below:

Portfolio_rtns.loc[Portfolio_rtns.index.repeat(2)]

Though I want to go from this:

               ZS          PANW        FTNT         JNPR    BAH           OKTA         AKAM  
2019-06-14  -0.025715   -0.010610   -0.011748   -0.010033   0.005554    -0.010740   -0.007198   
2019-06-14  -0.025715   -0.010610   -0.011748   -0.010033   0.005554    -0.010740   -0.007198   
2019-06-17  -0.011974   0.008412    -0.015586   -0.001501   0.001534    -0.008918   -0.025948   
2019-06-17  -0.011974   0.008412    -0.015586   -0.001501   0.001534    -0.008918   -0.025948   
2019-06-18  0.016419    0.001707    0.005635    0.009774    -0.001225   0.007277    0.021677    

to this:

               ZS           PANW        FTNT       JNPR         BAH         OKTA    AKAM    
2019-06-14  -0.025715   -0.010610   -0.011748   -0.010033   0.005554    -0.010740   -0.00719  
2019-06-17  -0.011974   0.008412    -0.015586   -0.001501   0.001534    -0.008918   -0.025948   
2019-06-18  0.016419    0.001707    0.005635    0.009774    -0.001225   0.007277    0.021677    
2019-06-14  -0.025715   -0.010610   -0.011748   -0.010033   0.005554    -0.010740   -0.007198      
2019-06-17  -0.011974   0.008412    -0.015586   -0.001501   0.001534    -0.008918   -0.025948   

...

How would I go about doing this? sort_index and sort_values did not seem to offer much help for duplicates.

CodePudding user response:

Use np.tile:

>>> df.loc[np.tile(df.index, 2)]
                  ZS      PANW      FTNT      JNPR       BAH      OKTA      AKAM
2019-06-14 -0.025715 -0.010610 -0.011748 -0.010033  0.005554 -0.010740 -0.007198
2019-06-17 -0.011974  0.008412 -0.015586 -0.001501  0.001534 -0.008918 -0.025948
2019-06-18  0.016419  0.001707  0.005635  0.009774 -0.001225  0.007277  0.021677
2019-06-14 -0.025715 -0.010610 -0.011748 -0.010033  0.005554 -0.010740 -0.007198
2019-06-17 -0.011974  0.008412 -0.015586 -0.001501  0.001534 -0.008918 -0.025948
2019-06-18  0.016419  0.001707  0.005635  0.009774 -0.001225  0.007277  0.021677

Setup MRE

data = \
{'ZS': {'2019-06-14': -0.025715, '2019-06-17': -0.011974, '2019-06-18': 0.016419},
 'PANW': {'2019-06-14': -0.01061, '2019-06-17': 0.008412, '2019-06-18': 0.001707},
 'FTNT': {'2019-06-14': -0.011748, '2019-06-17': -0.015586, '2019-06-18': 0.005635},
 'JNPR': {'2019-06-14': -0.010033, '2019-06-17': -0.001501, '2019-06-18': 0.009774},
 'BAH': {'2019-06-14': 0.005554, '2019-06-17': 0.001534, '2019-06-18': -0.001225},
 'OKTA': {'2019-06-14': -0.01074, '2019-06-17': -0.008918, '2019-06-18': 0.007277},
 'AKAM': {'2019-06-14': -0.007198, '2019-06-17': -0.025948, '2019-06-18': 0.021677}}

df = pd.DataFrame(data)
  • Related