Skip to content

Instantly share code, notes, and snippets.

@fnielsen
Last active November 16, 2019 04:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnielsen/9f31c35f773f21b8519d to your computer and use it in GitHub Desktop.
Save fnielsen/9f31c35f773f21b8519d to your computer and use it in GitHub Desktop.
Mixed indexing with integer index in Pandas DataFrame
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
@zjplab
Copy link

zjplab commented Nov 16, 2019

.ix is deprecated starting in 0.20.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment