AMIT BISHT
AMIT BISHT

Reputation: 59

TypeError: 'StringMethods' object is not subscriptable

I want to retreive the value from a dataframe column "age" data type as "Object". Age is in frequency for e.g, 10-20, 20-30.

I want to keep the upper end of the range.for e.g 20,30 etc.

I tried below logic:

df['age'] = df['age'].astype(str)
df['age'] = df['age'].str[1:].str.split('-', expand = True)[0]
df['age'] = df['age'].astype(int)

But getting the error as

"TypeError: 'StringMethods' object is not subscriptable"

What am I doing wrong?

Upvotes: 0

Views: 2165

Answers (1)

Sociopath
Sociopath

Reputation: 13401

I think you need:

df["age"] = df["age"].str.split("-").str[1]

Upvotes: 1

Related Questions