Search code examples
pythonpandasmulti-indexunpack

Multi-index for columns: how do I get # of columns per column header?


Each 30 minute interval is a column header. I want to have seven sub columns underneath each of them.

When I try to multi-index i get this error: Length mismatch: Expected axis has 13 elements, new values have 91 elements

How do I unpack this so that I get 91 columns?

coltime = pd.read_csv('mastercolumntimevalues.csv') #intervals range from 9:30 to 15:30#
coltime = pd.DataFrame(coltime)

thirtymindf = pd.DataFrame(columns=coltime)

thirtymindf.columns = pd.MultiIndex.from_product([thirtymindf.columns, ['open','high','low','close','vol','vwap','trades']])

thirtymindf.columns

update: This is what the csv file looks like:

columns of time values


Solution

  • IIUC and guess your csv file, try:

    lvl0 = pd.read_csv('mastercolumntimevalues.csv')['thirty'].dropna().tolist()
    lvl1 = ['open','high','low','close','vol','vwap','trades']
    mi = pd.MultiIndex.from_product([lvl0, lvl1])
    
    thirtymindf = pd.DataFrame(columns=mi)
    

    Output:

    >>> thirtymindf
    Empty DataFrame
    Columns: [(9:30, open), (9:30, high), (9:30, low), (9:30, close), (9:30, vol), (9:30, vwap), (9:30, trades), (10:00, open), (10:00, high), (10:00, low), (10:00, close), (10:00, vol), (10:00, vwap), (10:00, trades), (10:30, open), (10:30, high), (10:30, low), (10:30, close), (10:30, vol), (10:30, vwap), (10:30, trades), (11:00, open), (11:00, high), (11:00, low), (11:00, close), (11:00, vol), (11:00, vwap), (11:00, trades), (11:30, open), (11:30, high), (11:30, low), (11:30, close), (11:30, vol), (11:30, vwap), (11:30, trades), (12:00, open), (12:00, high), (12:00, low), (12:00, close), (12:00, vol), (12:00, vwap), (12:00, trades), (12:30, open), (12:30, high), (12:30, low), (12:30, close), (12:30, vol), (12:30, vwap), (12:30, trades), (13:00, open), (13:00, high), (13:00, low), (13:00, close), (13:00, vol), (13:00, vwap), (13:00, trades), (13:30, open), (13:30, high), (13:30, low), (13:30, close), (13:30, vol), (13:30, vwap), (13:30, trades), (14:00, open), (14:00, high), (14:00, low), (14:00, close), (14:00, vol), (14:00, vwap), (14:00, trades), (14:30, open), (14:30, high), (14:30, low), (14:30, close), (14:30, vol), (14:30, vwap), (14:30, trades), (15:00, open), (15:00, high), (15:00, low), (15:00, close), (15:00, vol), (15:00, vwap), (15:00, trades), (15:30, open), (15:30, high), (15:30, low), (15:30, close), (15:30, vol), (15:30, vwap), (15:30, trades)]
    Index: []