The Pandas method ix is deprecated as of version 0.20.0. If you want to index a DataFrame, you can use DataFrame.loc
for positional indexing and DataFrame.iloc
for label indexing.
This tutorial will go through how to solve this error with code examples.
Table of contents
AttributeError: ‘DataFrame’ object has no attribute ‘ix’
AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part of the error ‘DataFrame’ object has no attribute ‘ix‘ tells us that the DataFrame object we are handling does not have the ix
method as an attribute. The ix()
method is deprecated as of version 0.20.0, so if you are using a version after 0.20.0 you will get the AttributeError.
Example
Let’s look at an example where we want to select a subset of rows from a DataFrame. We will start with the dataset, which is a CSV file containing the names, charges and mass of four fundamental particles. We will save the dataset as particles.csv
.
name,charge,mass electron,-1, 0.511 muon,-1,105.7 tau,-1,1776.9 Z-boson,0,80433
Next, we will load the data into a DataFrame using pandas.
import pandas as pd df = pd.read_csv('particles.csv') subset = df.ix[[0,2],'name']
Let’s run the code to get the result:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [3], in <cell line: 1>() ----> 1 subset = df.ix[[0,2],'name'] File ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py:5583, in NDFrame.__getattr__(self, name) 5576 if ( 5577 name not in self._internal_names_set 5578 and name not in self._metadata 5579 and name not in self._accessors 5580 and self._info_axis._can_hold_identifiers_and_holds_name(name) 5581 ): 5582 return self[name] -> 5583 return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'ix'
We get an error because ix
is a deprecated Pandas method.
Solution #1: Use loc() to Index DataFrame
To solve this error we can use the loc()
method. The loc method provides access to a group of rows and columns by label(s) or a boolean array. Let’s look at the revised code:
import pandas as pd df = pd.read_csv('particles.csv') # Select appropriate indexes from the DataFrame index then use label indexing subset = df.loc[df.index[[0, 2]], 'name'] print(subset)
Let’s run the code to get the result:
0 electron 2 tau Name: name, dtype: object
We successfully retrieved the first and third rows of the name
column.
Solution #2: Use iloc() to index DataFrame
We can also use the iloc()
method to solve this error. The iloc method provides purely integer-location based indexing. To select rows of a specific column we need to provide a list of integers indicating positions and the integer location of the column using get_loc(). Let’s look at the revised code:
subset = df.iloc[[0,2], df.columns.get_loc('name')] print(subset)
Let’s run the code to get the result:
0 electron 2 tau Name: name, dtype: object
We can also select multiple columns by using the get_indexer() method, which returns the integer locations of the specified columns. Let’s look at the revised code:
# Get locations on the indexers and use positional indexing to select the rows subset = df.iloc[[0,2], df.columns.get_indexer(['name', 'charge'])] print(subset)
name charge 0 electron -1 2 tau -1
We successfully retrieved the first and third rows of the name
and charge
columns.
Summary
Congratulations on reading to the end of this tutorial! The method ix is deprecated as of pandas version 0.20.0. You can use DataFrame.loc
or DataFrame.iloc
instead.
For further reading on deprecated Pandas methods, go to the article:
To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.