Last active
November 16, 2019 04:00
-
-
Save fnielsen/9f31c35f773f21b8519d to your computer and use it in GitHub Desktop.
Mixed indexing with integer index in Pandas DataFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.DataFrame([[1, 2, 's'], [3, 4, 't'], [4, 5, 'u']], | |
index=[-1, 0, 1], columns=['a', 'b', 'c']) | |
>>> df.a # Correct type | |
-1 1 | |
0 3 | |
1 4 | |
Name: a, dtype: int64 | |
>>> df.loc[0, ['a', 'b']] # Wrong indexing | |
a 3 | |
b 4 | |
Name: 0, dtype: object | |
>>> df.ix[0, ['a', 'b']] # Wrong indexing | |
a 3 | |
b 4 | |
Name: 0, dtype: object | |
>>> df.iloc[0, :][['a', 'b']] # Correct indexing, wrong type | |
a 1 | |
b 2 | |
Name: -1, dtype: object | |
>>> df.loc[:, ['a', 'b']].iloc[0, :] # Correct indexing and type, but long | |
a 1 | |
b 2 | |
Name: -1, dtype: int64 | |
>>> df.ix[df.index[0], ['a', 'b']] # Ok | |
a 1 | |
b 2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.ix
is deprecated starting in 0.20.0