read_csv
requires the multi-index column labels to be non-sparse in order to properly parse them. I am not sure if this can be done automatically, though maybe a keyword could trigger this.
In [40]: def unsparsify_labels(index):
....: new_labels = []
....: for label in index.values:
....: if label[0].startswith('Unnamed'):
....: label = list(label)
....: label[0] = ll
....: label = tuple(label)
....: else:
....: ll = label[0]
....: new_labels.append(label)
....: return MultiIndex.from_tuples(new_labels)
....:
In [41]: unsparsify_labels(x)
Out[41]:
MultiIndex(levels=[[u'A', u'B'], [u'1', u'2']],
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
In [42]: data = """A,,B,
....: 1,2,1,2
....: 1,2,3,4
....: 5,6,7,8
....: """
In [43]: df = read_csv(StringIO(data),header=[0,1],index_col=None)
In [44]: df
Out[44]:
A Unnamed: 1_level_0 B Unnamed: 3_level_0
1 2 1 2
0 1 2 3 4
1 5 6 7 8
[2 rows x 4 columns]
In [45]: df.columns = unsparsify_labels(df.columns)
In [46]: df
Out[46]:
A B
1 2 1 2
0 1 2 3 4
1 5 6 7 8
[2 rows x 4 columns]
http://stackoverflow.com/questions/20473271/importing-multilevel-indexed-cvs-data-with-pandas-0-13/20474982#20474982
Comment From: jreback
@jtratner you are more familiar with mi....do we have something like this already?
Comment From: mroeschke
Looks like there hasn't been much appetite for this feature in a while so closing for now. Can reopen if there's interest