1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.