Search code examples
pythonpandasdataframemulti-index

Copy a Multi-Index column of a Pandas Dataframe including the second header


Background - I have a dataset with two headers that I've read from a CSV...

df = pd.read_csv(file, header=[0,1])
print(df)

     A   B   C
     a   b   c
---------------
0    1   2   3
1    4   5   6
2    7   8   9
       ...

What I Want - I'd like to duplicate one of the columns (say, A) into a new column D, such that...

     A   B   C   D
     a   b   c   a
----------------------
0    1   2   3   1
1    4   5   6   4
2    7   8   9   7
       ...

What I'm Getting - ...but when I do this using df['D'] = df['A'], what I get is a copy without the content of the second header...

     A   B   C   D
     a   b   c   
----------------------
0    1   2   3   1
1    4   5   6   4
2    7   8   9   7
       ...

TLDR - How can I capture the content of the second header of my multi-index dataframe when copying a column?


Solution

  • Need to specify both levels of multiindex

    # assign ('A', 'a') values to ('D', 'a') column
    df[('D','a')] = df[('A','a')] # or df['D','a'] = df['A']
    

    enter image description here