Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
DataFrame.ix[]
was once a popular method in pandas for label and integer-based indexing. However, it was deprecated in version 0.20.0 (in April 2017) due to confusion on whether to interpret an integer as a location or a label. Thus, for modern pandas usage, it's recommended to use .loc[]
for label-based indexing and .iloc[]
for integer-based indexing.
Nevertheless, for the sake of understanding, I'll illustrate how .ix[]
was used and then provide the modern alternatives.
Let's first set up a sample dataframe:
import pandas as pd df = pd.DataFrame({ 'A': ['A0', 'A1', 'A2', 'A3'], 'B': ['B0', 'B1', 'B2', 'B3'], 'C': ['C0', 'C1', 'C2', 'C3'], 'D': ['D0', 'D1', 'D2', 'D3'] }, index=['foo', 'bar', 'baz', 'qux']) print(df)
.ix[]
(Deprecated):# Using .ix with labels (rows and columns) print(df.ix['foo':'bar', 'A':'B'])
# Using .ix with integer indexes (rows and columns) print(df.ix[0:2, 0:2])
# Using .ix with mixed indexes print(df.ix['foo':2, 0:2]) # This was confusing and one of the reasons .ix was deprecated
.loc[]
for label-based slicing:print(df.loc['foo':'bar', 'A':'B'])
.iloc[]
for integer-based slicing:print(df.iloc[0:2, 0:2])
It's recommended to avoid .ix[]
in new code and to use .loc[]
and .iloc[]
instead. They are explicit and thus reduce ambiguity, leading to clearer, more maintainable code.
In summary, while .ix[]
provided a way to mix label-based and integer-based indexing in a single method, it was often a source of confusion. The explicit nature of .loc[]
and .iloc[]
provides clarity and minimizes potential pitfalls.
Label and integer-based slicing with DataFrame.ix[]:
.ix[]
method allowed mixed integer and label-based indexing.sliced_data = df.ix[1:3, 'Column_Name']
Using ix[] for slicing in Pandas:
.ix[]
for slicing rows and columns.sliced_data = df.ix[1:3, 'Column_Name']
Pandas .ix[] deprecated replacement:
.ix[]
is deprecated, use .loc[]
for label-based indexing and .iloc[]
for integer-based indexing.label_based = df.loc[:, 'Column_Name'] integer_based = df.iloc[:, 1]
Migrating from .ix[] to .loc[] and .iloc[] in Pandas:
.ix[]
to .loc[]
and .iloc[]
for improved clarity.label_based = df.loc[:, 'Column_Name'] integer_based = df.iloc[:, 1]
Label-based and integer-based indexing in Pandas DataFrame:
.loc[]
for label-based indexing and .iloc[]
for integer-based indexing.label_based = df.loc[:, 'Column_Name'] integer_based = df.iloc[:, 1]
Deprecated methods in Pandas DataFrame indexing:
.ix[]
is deprecated, and its usage is discouraged.# Avoid using .ix[]
Alternatives to .ix[] in Pandas:
.ix[]
with .loc[]
for label-based indexing and .iloc[]
for integer-based indexing.label_based = df.loc[:, 'Column_Name'] integer_based = df.iloc[:, 1]
Pandas ix[] method examples:
.loc[]
and .iloc[]
instead.# Deprecated sliced_data = df.ix[1:3, 'Column_Name']