I have a bunch of Pandas code that uses tuples as indices. I've recently come across the need to access an individual element of a DataFrame with DataFrame.ix, which is getting confused by the tuples. It seems to think my tuple is a sequence of keys I want to access, not a single keys (which happens to be a sequence) that I want to access. How can I extract an individual row for which a tuple is the key?
Perhaps this is a cautionary tale not to use sequences in a Pandas index, but in my case it's too late.
import string, pandas as pd, numpy as np
bar = pd.DataFrame(np.random.random((8,2)))
bar.columns = ['col1', 'col2']
bar.index = list(string.ascii_lowercase)[:8]
print bar
print bar.iloc[0].name
print bar.ix[bar.iloc[0].name]
bar.index = [tuple(list(string.ascii_lowercase)[i:i+3]) for i in range(8)]
print bar.iloc[0].name
print bar.ix[bar.iloc[0].name] # Fails with `KeyError: 'a'`
ixsince it conforms more narrowly to the question as asked. But is either of the two preferred?xsmay be preferable here. The indexers (ix,iloc,loc) can do a lot more, but because of the syntax ambiguity between tuple keys and MultiIndex selection, it's likely less error prone.