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

Add index column wich is under a hierarchical index PANDAS/python

I want set the col1 as Index. The dataframe is the result of a concatenation and I using keys for created hierarchical index

import pandas as pd

d = {'col1': [0, 1, 2, 3], 'col2': pd.Series([2, 3], index=[2, 3])}
df = pd.DataFrame(data=d, index=[0, 1, 2, 3])

d2 = {'col1': [0, 1, 4, 3], 'col2': pd.Series([4, 3], index=[2, 3])}
df2 = pd.DataFrame(data=d2, index=[0, 1, 2, 3])

result = pd.concat([df,df2], axis=1, keys=['PartieA', 'PARTIEB'])
print(result)
result.set_index(['col1'], append=True)
print(result)

I got this error :

KeyError: "None of ['col1'] are in the columns"

I have 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

PartieA      PARTIEB     
     col1 col2    col1 col2
0       0  NaN       0  NaN
1       1  NaN       1  NaN
2       2  2.0       4  4.0
3       3  3.0       3  3.0

I want:


          PartieA      PARTIEB     
          col2    col1 col2
    col1  NaN       0  NaN
       0  NaN       1  NaN
       1  2.0       4  4.0
       2  3.0       3  3.0

>Solution :

Select MultiIndex by tuple and then rename index name by DataFrame.rename_axis:

result = result.set_index([('PartieA','col1')]).rename_axis('col1')
print(result)

     PartieA PARTIEB     
        col2    col1 col2
col1                     
0        NaN       0  NaN
1        NaN       1  NaN
2        2.0       4  4.0
3        3.0       3  3.0

Because index is same like original, aslo is possible remove column by tuple:

result = result.drop([('PartieA','col1')], axis=1).rename_axis('col1')
print(result)
     PartieA PARTIEB     
        col2    col1 col2
col1                     
0        NaN       0  NaN
1        NaN       1  NaN
2        2.0       4  4.0
3        3.0       3  3.0
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