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

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…

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

     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

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