I think this is a bug in to_csv . If you are looking for workarounds, then here are a couple.
For reading in this csv specify header lines *:
In [11]: csv = "AA,BB,CC DD,EE,FF ,, a,b,c1 a,b,c2 a,b,c3" In [12]: pd.read_csv(StringIO(csv), header=[0, 1]) Out[12]: AA BB CC DD EE FF 0 ab c1 1 ab c2 2 ab c3
* strange it seems to ignore empty lines.
To record, you can record the title first and then add:
with open('test.csv', 'w') as f: f.write('\n'.join([','.join(h) for h in zip(*df.columns)]) + '\n') df.to_csv('test.csv', mode='a', index=False, header=False)
Note the to_csv part for the MultiIndex column:
In [21]: '\n'.join([','.join(h) for h in zip(*df.columns)]) + '\n' Out[21]: 'AA,BB,CC\nDD,EE,FF\n'
source share