I feel some strange pandas
behavior here. I have a dataframe that looks like
df = pd.DataFrame(columns=['Col 1', 'Col 2', 'Col 3'], index=[('1', 'a'), ('2', 'a'), ('1', 'b'), ('2', 'b')]) In [14]: df Out[14]: Col 1 Col 2 Col 3 (1, a) NaN NaN NaN (2, a) NaN NaN NaN (1, b) NaN NaN NaN (2, b) NaN NaN NaN
I can set the value of an arbitrary element
In [15]: df['Col 2'].loc[('1', 'b')] = 6 In [16]: df Out[16]: Col 1 Col 2 Col 3 (1, a) NaN NaN NaN (2, a) NaN NaN NaN (1, b) NaN 6 NaN (2, b) NaN NaN NaN
But when I go to the link to an element that I just installed using the same syntax, I get
In [17]: df['Col 2'].loc[('1', 'b')] KeyError: 'the label [1] is not in the [index]'
Can someone tell me what I'm doing wrong or why is this happening? Am I just not allowed to set the index as a multi-element tuple?
Edit
It seems that wrapping the tuple index on the list works.
In [38]: df['Col 2'].loc[[('1', 'b')]] Out[38]: (1, b) 6 Name: Col 2, dtype: object
Despite the fact that I'm still getting some kind of strange behavior in my actual use case, it would be nice to find out if this is recommended.