AttributeError: ‘DataFrame’ object has no attribute ‘ix’ [Solved]

When you are working with Pandas DataFrame and want to select data using .ix attribute, you might face AttributeError: ‘DataFrame’ object has no attribute ‘ix’ error. The reason for getting this error is because of the new version of the pandas. The .ix attribute has been depreciated in the Pandas version greater than 0.20.0. So, if you are using a version of pandas greater than 0.20.0, then you face this error. In this short article, we will learn how we can solve the AttributeError: ‘DataFrame’ object has no attribute ‘ix’ error using different methods. Moreover, we will also learn various methods to select items from the data frame.

AttributeError: ‘DataFrame’ object has no attribute ‘ix’ – Possible Solutions

If you are working with pandas and the version of pandas is greater than 0.20.0, but you are still using the.ix attribute to select the elements, then you will face AttributeError: ‘DataFrame’ object has no attribute ‘ix’ error. The reason for this error is because .ix attribute is no more available in updated versions of pandas and there are different methods and attributes to select the elements from the DataFrame.

Lets us first take an example to see, when and how we are getting this error.

# importing the modules
import pandas as pd
import numpy as np

# creating dataset
data = {"name":["A","B","C","D"],"age":[23,25,26,32],
        "country":["UK","Jaoan","France","Germany"]}

# converting the data to data frame
df = pd.DataFrame(data)

# selecting data frame
df.ix[:2,]

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_519106/1811320355.py in <module>
     11 
     12 # selecting data frame
---> 13 df.ix[:2,]

~/.local/lib/python3.10/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5905         ):
   5906             return self[name]
-> 5907         return object.__getattribute__(self, name)
   5908 
   5909     def __setattr__(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'ix'

As you can see, we got the error because we are using an updated version of pandas. You can use the __version__ method to get the version of the installed pandas on your system.

# importing the pandas
import pandas

# printing the version
print(pandas.__version__)

Output:

1.5.0

Notice that we have an updated version of pandas and that is the reason for getting AttributeError: ‘DataFrame’ object has no attribute ‘ix’ error because the .ix attribute is no more in the updated versions of the pandas.

Solution-1: Using iloc[] method

The iloc[] method in Pandas helps us to select a specific column or row from the data frame. This method uses index values to retrieve any particular value from the data frame. We can use .iloc[] method instead of .ix to get access to the elements in the data frame.

Let us see how we can use the iloc[] method to select the first two rows:

# importing the modules
import pandas as pd
import numpy as np

# creating dataset
data = {"name":["A","B","C","D"],"age":[23,25,26,32],
        "country":["UK","Jaoan","France","Germany"]}

# converting the data to data frame
df = pd.DataFrame(data)

# selecting data frame
df.iloc[:2,]

Output:

AttributeError: 'DataFrame' object has no attribute 'ix'

As you can see, we got the first two rows with all columns.

If we want to access the first two rows and first two columns only, we can also do that using the .loc[] method as shown below:

# selecting data frame
df.iloc[:2,:2]

Output:

AttributeError: 'DataFrame' object has no attribute 'ix'-data frame

As you can see, we were able to select the first two rows and columns.

If you want to access values of only one column, you can do that by using the following code. In this example, we will get access to the first column of the data frame.

# selecting data frame
df.iloc[:,:1]

Output:

AttributeError: ‘DataFrame’ object has no attribute ‘ix’

Notice that the output contains all the elements of the specified column.

Solution-2: Using loc[] method

The loc[] method is similar to iloc[] method in terms that they are both used to access the elements from the data frame. But unlike iloc[] method, the loc[] method accesses a group of rows and columns by their labels or boolean array.

Let us take an example and see how we can use the loc[] method in order to select a single row. Let us select the second row from the data frame using the loc[].

# importing the modules
import pandas as pd
import numpy as np

# creating dataset
data = {"name":["A","B","C","D"],"age":[23,25,26,32],
        "country":["UK","Jaoan","France","Germany"]}

# converting the data to data frame
df = pd.DataFrame(data)

# selecting data frame
df.loc[1]

Output:

name           B
age           25
country    Jaoan
Name: 1, dtype: object

Notice that we got all the elements from the second row using the loc[] method.

We can also use the loc[] method to get multiple rows as well as shown below:

# selecting data frame
df.loc[[1, 2]]

Output:

AttributeError: ‘DataFrame’ object has no attribute ‘ix’

Hopefully, these methods were useful and helped you to get rid of AttributeError: ‘DataFrame’ object has no attribute ‘ix’ error.

Solution-3: Using [] operators to select the columns

If you want to select the columns from the data frame then simply you can use the [] operators to select the columns.

For example, let us select the age column from the data frame.

# importing the modules
import pandas as pd
import numpy as np

# creating dataset
data = {"name":["A","B","C","D"],"age":[23,25,26,32],
        "country":["UK","Jaoan","France","Germany"]}

# converting the data to data frame
df = pd.DataFrame(data)
# selecting data frame
df["age"]

Output:

0    23
1    25
2    26
3    32
Name: age, dtype: int64

Moreover, we can also use the [] operators to select multiple columns as well.

# selecting data frame
df[["age", 'country']]

Output:

AttributeError: ‘DataFrame’ object has no attribute ‘ix’-solved

Hopefully, you will now have an idea of how to get rid of AttributeError: ‘DataFrame’ object has no attribute ‘ix’ error and you can easily solve it using any of the given methods.

Summary

In this short article, we discussed how we can solve the AttributeError: ‘DataFrame’ object has no attribute ‘ix’ error using various methods. We discussed three methods to select the rows and columns from the data frame.

Related Issues:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top