I'm sensing some weird 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 reference the element that I just set 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 this behavior occurs? Am I simply not allowed to set the index as a multi-element tuple?
Edit
Apparently, wrapping the tuple index in a list works.
In [38]: df['Col 2'].loc[[('1', 'b')]]
Out[38]:
(1, b) 6
Name: Col 2, dtype: object
Although I'm still getting some weird behavior in my actual use case so it'd be nice to know if this is not recommended usage.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…