Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • Python tutorial: Pandas groupby ( Video 1) Python
  • Python tutorial: Create an input box in Tkinter Python
  • python classes class
  • Python Tutorial: How to validate data using tuples Python Tuples
  • TypeError: ‘str’ object is not callable Python Functions
  • how to remove unwanted characters from your data R Programming
  • ValueError: invalid literal for int() with base 10 Value Error
  • how to create an instance of a class class

IndexError: index 2 is out of bounds for axis 0 with size 2

Posted on April 4, 2021October 25, 2022 By admin 2 Comments on IndexError: index 2 is out of bounds for axis 0 with size 2

In Python you may come across IndexError: index 2 is out of bounds for axis 0 with size 2 from time to time.

The error relates to the use of indexes and the no of columns you are referencing.

We discussed single positional indexer is out-of-bounds and in that blog post, it was related to referencing indexes for rows that do not exist.

In this blog post, the error relates to referencing the index for columns that do not exist.

Lets walk through the code below and where the problem arises

So let us refresh ourselves with the data that is in the excel CSV file below:

import pandas as pd
dataset = pd.read_csv('import_file.csv', sep = ',')
df = pd.DataFrame(dataset, columns=['Name','Age'])
a = df.iloc[4][2] #===>this allows you to print a particular row or value in that row
print(df)
print(a)
Output:
Traceback (most recent call last):
  File "C:/Users/haugh/OneDrive/dataanalyticsireland/YOUTUBE/IndexError_index_2_is_out_of_bounds_for_axis_0_with_size_2/index_2_out_of_bounds_for_axis_0_with_size_2.py", line 7, in <module>
    a = df.iloc[4][2] #===>this allows you to print a particular row or value in that row
  File "C:\Users\haugh\anaconda3\lib\site-packages\pandas\core\series.py", line 879, in __getitem__
    return self._values[key]
IndexError: index 2 is out of bounds for axis 0 with size 2

As you can see the column index value can be either 0 or 1, this is because they represent the index values of the columns starting at 0 and ending at 1.

The problem arises here in this line ===> a = df.iloc[4][2] . Essentially the value is looking to reference a column with index value 2, but as we saw above that index value does not exist.

As a result by replacing it with a column index value within the proper range ( in this case 0 or 1), the error will disappear and the code will work as expected.

import pandas as pd
dataset = pd.read_csv('import_file.csv', sep = ',')
df = pd.DataFrame(dataset, columns=['Name','Age'])
a = df.iloc[4][0] #===>this allows you to print a particular row or value in that row
print(df)
print(a)
Gives:
       Name  Age
0       Joe   21
1      John   22
2       Jim   23
3      Jane   24
4  Jennifer   25
Jennifer
OR
import pandas as pd
dataset = pd.read_csv('import_file.csv', sep = ',')
df = pd.DataFrame(dataset, columns=['Name','Age'])
a = df.iloc[4][1] #===>this allows you to print a particular row or value in that row
print(df)
print(a)
Gives:
      Name  Age
0       Joe   21
1      John   22
2       Jim   23
3      Jane   24
4  Jennifer   25
25
Process finished with exit code -1073741819 (0xC0000005)

Finally, in a = df.iloc[4][1] you can also change the value 4, which is the index for that row to either 0,1,2,3 and the code will work with no errors as it brings back the values that you expect.

So in summary:

(A) This error happens when you try to use a column index value does not exist as it is outside the index values for the columns that are in the data set.

(B) If this error does occur always check the expected index values for each column and compare against what you are trying to return.

Index Error Tags:dataframe, iloc, index value, IndexError, out of bounds, pandas, Python

Post navigation

Previous Post: IndexError: single positional indexer is out-of-bounds
Next Post: How to Group By in a Select Statement

Related Posts

  • IndexError: list index out of range Index Error
  • IndexError: single positional indexer is out-of-bounds Index Error

Comments (2) on “IndexError: index 2 is out of bounds for axis 0 with size 2”

  1. Munier Ahmed Elsherif says:
    August 24, 2021 at 11:46 pm

    Hi Sir,
    can you please help me with this?

    _________________________________________
    import csv
    import numpy as np
    from matplotlib import pyplot as plt

    t0 = 0.0
    tf = 5000.0
    n = 5001
    dt = (tf-t0)/(n-1)
    t = np.linspace (t0,tf,n)
    g = 0.1
    Kp = -2.0
    Ti = 10
    Td = 0.0
    SP = 50.00
    h = np.zeros([n])
    fi = np.zeros([n])
    fo = np.zeros([n])
    h[0] = 50.0

    f = open(‘lfsretunepiddat.csv’)
    csv_f = csv.reader(f)
    nline = 0

    for field in csv_f:
    nline = nline + 1
    fi[nline] = field[0]
    f.close()

    for i in range (1,n):
    h[i] = h[i-1] + g*(fi[i-1] – fo[i-1]) * dt
    fo[i] = fo[i-1] + (Kp)*((1.0 + dt/Ti + Td/dt)*(SP – h[i]) + (-1.0 – 2.0*Td/dt)*(SP – h[i-1]) + Td/dt * (SP – h[i-2]))

    for i in range (n):
    print (t[i],h[i],fi[i],fo[i])

    plt.plot(t,fi,’go–‘, linewidth=1, markersize=0.8 )
    plt.plot(t,fo,’ro–‘, linewidth=1, markersize=0.1 )
    plt.plot(t, h,’yo–‘, linewidth=1, markersize=0.1 )
    plt.xlabel(“Value of t”)
    plt.ylabel(“Value of fi,fo,h”)
    plt.title (“Level Flow Smoothing PID Simulation”)
    plt.show ()

    Reply
    1. admin says:
      August 30, 2021 at 10:33 pm

      Hi,

      Thanks for your message. Can you please explain exactly what your problem is? Also I will need to know what is inside the csv files you are opening, so I can look recreate the problem.

      Data Analytics Ireland

      Reply

Leave a Reply Cancel reply

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

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • How To Run Python Validation From Javascript Javascript
  • IndexError: single positional indexer is out-of-bounds Index Error
  • YouTube channel lists – Tuples Python Tuples
  • Create a HTML Table From Python Using Javascript Javascript
  • How To Join Tables In SQL SQL
  • python constructor self and __init__explained Python
  • how to select columns with SQL SQL
  • How to show percentage differences between files in Python CSV

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT