1

Refer here for the question background. I want to add C to only column B.

I need output as:

 df
    Out[92]: 
       A  B
          C
    a  0  0
    b  1  1
    c  2  2
    d  3  3
    e  4  4

I tried this example as :

dfnew=pd.DataFrame({'a':[1,2,3],'b':[4,5,6]})

columns=[('c','b')]  #changed from columns=[('c','a'),('c','b')]

dfnew.columns=pd.MultiIndex.from_tuples(columns)

But that doesn't works. ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements

yatu
  • 86,083
  • 12
  • 84
  • 139
Rex5
  • 771
  • 9
  • 23
  • If someone has better approach(like some manipulation to pd.MultiIndex) pls do post ! Will be accepted if better than accepted answer. Thanks ! – Rex5 May 27 '19 at 10:22

1 Answers1

2

You can use MultiIndex.from_arrays:

df.columns = pd.MultiIndex.from_arrays([df.columns, ['','C']])

   A  B
      C
a  0  0
b  1  1
c  2  2
d  3  3
e  4  4

Note that pd.MultiIndex.from_tuples is expecting a list of tuples, as the name suggests. If you check the source code, you'll see that if that is not the case, it will create one from the nested list by zipping it:

list(zip(*[df.columns, ['','C']]))
# [('A', ''), ('B', 'C')]

Which is the reason why you don't get what you expect.


If you want to do the same by specifying a list of columns, you could do:

cols = [(i, 'C') if i in ['B','D'] else (i, '') for i in df.columns]
# [('A', ''), ('B', 'C'), ('C', ''), ('D', 'C')]
df.columns = pd.MultiIndex.from_tuples(cols)

   A  B  C  D
      C     C
a  0  0  0  0
b  1  1  1  1
c  2  2  2  2
d  3  3  3  3
e  4  4  4  4
yatu
  • 86,083
  • 12
  • 84
  • 139
  • Can we place for the nth column ? or by specific column name like in this case can we specify that `C` should be placed under `B` only ? Because it would be quite troublesome if there are many columns before `B` which there are in my actual case ! – Rex5 May 27 '19 at 10:00
  • 1
    Updating soon @Rex5 – yatu May 27 '19 at 10:02
  • 1
    Check now @Rex5 – yatu May 27 '19 at 10:06
  • 1
    that's good yet if you find some other method do update please! thanks ! – Rex5 May 27 '19 at 10:09