Fix TypeError: int object is not Subscriptable

In this post, we will learn how to Fix TypeError: int object is not Subscriptable Python. The Python object strings, lists, tuples, and dictionaries are subscribable because they implement the __getitem__ method and the subscript operator [] is used to access the element by index in Python from subscriptable objects. Whereas integer is not a Subscriptable object and is used to store the whole number. An integer number can’t be accessed by index.

1. What is TypeError: int object is not Subscriptable


Whenever we perform a subscriptable objects operation on an integer like treating an integer variable as an array and accessing an element using subscript operator [] then “TypeError: int object is not Subscriptable” is encountered.

In the below example we have demonstrated TypeError: ‘int’ object is not Subscriptable, We have asked the user to input some integer value and display it using a print statement. But when we have accessed the integer value using subscript operator [] then TypeError is encountered because we are treating the integer variable as an array.

number = int(input("Please enter a integer value: "))
print("You have entered:", number)
print( number[0])

Output

print( number[0])
TypeError: 'int' object is not subscriptable

2. Solution to TypeError:‘int’ object is not Subscriptable


In the above example, we have taken input from the user and converted it into an integer by using the python built-in function int() and tried to access it by using the Index position then we encountered with TypeError the solution for the type error not converting the input value to an integer or can be resolved by avoiding slicing and indexing while the accessed value of integer object.

number = input("Please enter a integer value: ")
print("You have enetered:", number)
print( number[0])

Output

Please enter a integer value: 36912
You have enetered: 36912
3

3. TypeError:‘int’ object is not Subscriptable Pandas


In this example, we are finding the sum of Pandas dataframe column “Marks” that has int type. While applying the Lambda function on the ‘Marks’ column using index notation or subscript operator and encountering with TypeError: ‘int’ object is not subscriptable in Pandas.

import pandas as pd
 
data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)
print(dfobj.dtypes)
 
MarksGross = lambda x: int(x[1])
dfobj.Marks = dfobj.Marks.apply(MarksGross)

Print(dfobj.Marks.sum())

Output

  MarksGross = lambda x: int(x[1])
TypeError: 'int' object is not subscriptable

4. How to fix TypeError: int object is not Subscriptable Pandas


To solve Type Error with Pandas dataframe, We have not applied the lambda function using index notation instead use int(x) pass value of x inside () brackets.

import pandas as pd
 
data = {
    'Name': ['Jack', 'Jack', 'Max', 'David'],
    'Marks':[97,97,100,100],
    'Subject': ['Math', 'Math', 'Math', 'Phy']
}
 
dfobj = pd.DataFrame(data)
print(dfobj.dtypes)
 
MarksGross = lambda x: int(x)
dfobj.Marks = dfobj.Marks.apply(MarksGross)

print(dfobj.Marks.sum())

Output

Name       object
Marks       int64
Subject    object
dtype: object
394

Summary

In this post, we have learned how to fix TypeError: int object is not Subscriptable with examples. This error occurs when we try to access an integer variable value by index.