Read csv file with many named column labels with pandas Read csv file with many named column labels with pandas pandas pandas

Read csv file with many named column labels with pandas


You can use the header, index_col and tupleize_cols arguments of read_csv:

In [1]: df = pd.read_csv('foo.csv', header=[0, 1, 2], index_col=[0, 1], tupleize_cols=False, sep='\s*,\s+')

Note: in 0.13 tupelize=False will be the default, so you won't need to use that.

There's a little bit of hacking required to get out the column level names:

In [2]: df.columns.names = df.columns[0]In [3]: del df[df.columns[0]]In [4]: dfOut[4]:colLabel:name           dog         bat    OstrichcolLabel:genus        Canis  Chiroptera  Struthio,colLabel:activity   diurnal   nocturnal    diurnalday    time of dayMonday morning           17           5          2       day               63           0         34       night             21          68          1Friday day               72           0         34