Jason Clarkson
Jason Clarkson

Reputation: 111

Indexing a multi-indexed dataframe using a list

I have a multi-indexed pandas dataframe created like this:

m_index = ['time_remaining', 'inventory']
for i in indicators:
    m_index.append(i.name)
cols = []
for col in m_index:
    cols.append(col)
cols.append('action')
cols.append('cost')

optimal_actions = pd.DataFrame(columns=cols)
optimal_actions.set_index(m_index, inplace=True)

I then have a list of values for the index:

state_variables = [indicator.value for indicator in indicators]
state = [time_remaining, i*trade_size]      
state.extend(state_variables)

And I am trying to set the row indexed by the list of values to a value:

state = [300, 1, 1.0]
optimal_actions.loc[state] = [-1, -0.2]

This is giving me a nasty error:

ValueError: zero-size array to reduction operation maximum which has no identity

Any help would be much appreciated!

Upvotes: 1

Views: 42

Answers (1)

Jason Clarkson
Jason Clarkson

Reputation: 111

If anyone needs this, I ended up solving it by indexing the multi-index by a tuple rather than a list. So the modifcation to the above code would be:

state = [300, 1, 1.0]
optimal_actions.loc[tuple(state)] = [-1, -0.2]

Upvotes: 1

Related Questions