pandas index and columns in same line

Viewed 57

I have multindex with two columns in a dataframe as follow

import numpy as np
import pandas as pd
df = pd.DataFrame(np.array([[1, 2, 3, 0], [1, 5, 6, 0], [2, 5, 6, 0]]),columns=['a','b','c', 'd'])
df

df.set_index(['a', 'b'],inplace=True)
df

That produces index in a different line of column header

     c  d
a b      
1 2  3  0
  5  6  0
2 5  6  0

How index and column can be put in the same line without missing index columns?

Desired output, index and columns in the same line

a b  c  d    
1 2  3  0
  5  6  0
2 5  6  0
1 Answers

its true, df.set_index(['a', 'b'],inplace=True).

then you used "print", printing change view(Change is only in vision.). if you save dataframe, its save true.

enter image description here

Related