TypeError:object is not subscriptable Python

In this post, we will learn how to fix TypeError:object is not subscriptable error in Python. The TypeError is raised when trying to use an illegal operation on non subscriptable objects(set,int,float) or does not have this functionally. Python throws the TypeError object is not subscriptable if We use indexing with the square bracket notation on an object that is not indexable.

1.TypeError:object is not subscriptable Python


In this Python program example, accessing the integer variable values, accessing with the square bracket notation and since is not indexable.

number = 56789
print( number[0])

Output

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

Solution TypeError:object is not subscriptable Python

We have to access the integer variable without index notation.Let us understand with the below example

number = 56789
print( ‘The value is :’,number)

Output

'The value is  : 56789

2.TypeError:object is not subscriptable set


We can’t access the set element by using the index notation.Running the below code will throw TypeError.To solve this error We have to use for loop to iterate over the set and display a single value.The Python object strings, lists, tuples, and dictionaries are subscribable their elements can be accessed using the index notation.

mySet = {3,6,9,12,15,18,121}
print(mySet[0]) 

Output

print(mySet[0])
TypeError: 'set' object is not subscriptable

Solution object is not subscriptable set

In this example we have for loop to iterate over the set and display each element in the set.

mySet = {3,6,9,12,15}
for inx in mySet:
print(inx,end=”,”)

Output

3,6,9,12,15,

3.TypeError: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