Home > Enterprise >  Add index column which is under a hierarchical index PANDAS/python
Add index column which is under a hierarchical index PANDAS/python

Time:05-25

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:

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

CodePudding user response:

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
  • Related