How to Fix the “IndexingError: Too Many Indexers” in Python?

How to Reverse Range in Python?

Are you getting the “IndexingError: too many indexers” error while accessing data from a DataFrame using Python’s loc() function?

The loc() function of Pandas DataFrame is used to access an individual or a group of rows or columns by the label(s) or a boolean array. It is used for different purposes in Python programming; a few key uses of the loc[] properties are the following;

  1. It is used for selecting or filtering the data in the DataFrame.
  2. It works with indices as well; each index represents a row in the DataFrame
  3. It also works with ranges to divide the whole data into small chunks.

 

In this article, we’ll discuss the IndexingError: too many indexers and the cause of it. Furthermore, we’ll discuss how to fix the IndexingError in Python. So let’s dive deep into it and learn how to fix the IndexingError using the loc[] property of Pandas Python.

 

 

What is the “IndexingError: Too Many Indexers” Error in Python?

Pandas DateFrame represents data in a tabular structure where the data is stored in rows and columns. We can use the two functions loc and iloc() to access rows, columns, or some specific entities of the DataFrame.

But before going into the details, first let’s see why we get the IndexingError: too many indexers.

 

Code

# import libraries

import pandas as pd




# create a DataFrame

df = pd.DataFrame({'A' : ['a1', 'a2', 'a3', 'a4'],

                   "B" : ['b1', 'b2', 'b3', 'b4'],

                   "C" : ['c1', 'c2', 'c3', 'c4'],

                   "D" : ['d1', 'd2', 'd3', 'd4']})




print("***** Data *****")

print(df)

df.loc[1,2,3] # IndexingError: too many indexers

 

Output

IndexingError too many indexers in Python

 

We are getting in the IndexingError because of the multiple indexes provided to loc(), which isn’t supported by the functions. Instead you should provide a range of rows or define the specific rows as a list.

 

 

How to Fix the “IndexingError: Too Many Indexers” Error in Python?

The reason behind getting the error is that you have provided invalid indexes to the loc() function. Instead, if you want to get multiple rows or columns, you can provide a range to the loc() as follow:

 

Code

import pandas as pd




df = pd.DataFrame({'A' : ['a1', 'a2', 'a3', 'a4'],

                   "B" : ['b1', 'b2', 'b3', 'b4'],

                   "C" : ['c1', 'c2', 'c3', 'c4'],

                   "D" : ['d1', 'd2', 'd3', 'd4']})

print("***** Data *****")

 

# print the row 0 to 3 from the DataFrame

print(df.loc[0:3]) 


 

Output

***** Data *****

     A B C D

0 a1 b1 c1 d1

1 a2 b2 c2 d2

2 a3 b3 c3 d3

3 a4 b4 c4 d4

 

As you can see in the above example, we have provided a range to the loc() function to print rows from row number 0 to 3. Remember that row numbering works like the index of an array, which means the number 0 is counted as 1.

 

We can further provide customized parameters to the loc() to deal with the DataFrame accordingly. For example, let’s say we want to print all entities of column ‘A’:

 

Code

import pandas as pd




df = pd.DataFrame({'A' : ['a1', 'a2', 'a3', 'a4'],

                   "B" : ['b1', 'b2', 'b3', 'b4'],

                   "C" : ['c1', 'c2', 'c3', 'c4'],

                   "D" : ['d1', 'd2', 'd3', 'd4']})

print("***** Data *****")

 # print all entities of column A

# loc[condition 1 on to select number of rows, column name]

df.loc[:, 'A']

 

Output

***** Data *****

0 a1

1 a2

2 a3

3 a4

Name: A, dtype: object

 

In the above code, we have provided tow parameters to the loc() functions as loc[ : , ‘A’ ]. The first parameter defined the range of rows, whereas the second defined the column name. The colon ‘:’ means all rows of column ‘A’.

 

Now let’s say you want to display the 1st and 3rd rows on the same DataFrame:

 

Code

import pandas as pd




df = pd.DataFrame({'A' : ['a1', 'a2', 'a3', 'a4'],

                   "B" : ['b1', 'b2', 'b3', 'b4'],

                   "C" : ['c1', 'c2', 'c3', 'c4'],

                   "D" : ['d1', 'd2', 'd3', 'd4']})

print("***** Data *****")

 

# print the 1st and 3rd rows

print(df.loc[[0,2]])

 

Output

***** Data *****

    A B C D

0 a1 b1 c1 d1

2 a3 b3 c3 d3

 

 

Conclusion

To summarize the article on how to fix the “IndexingError: too many indexers” error, we have discussed the reason for getting it. Furthermore, we’ve discussed its solution and different practical examples.

In addition, we’ve seen how to filter out the DataFrame to get results according to your need, like displaying the data based on columns, rows, or specified rows. 

Happy Learning! 😊

To explore more, comment below on how to print the data of a DataFrame using two columns.

Total
0
Shares
Leave a Reply

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

Related Posts
Total
0
Share