'int' object has no attribute 'split' , but it's an object

Viewed 34

So I have a dataframe that one of it's columns is 'Number of Pages' and the values are in this format:

number + "Pages" .. e.g : 300 Pages

I want the column to be an 'int' datatype instead of object, so I wanted to split based on the space and take the first argument .. (Which By the way, worked for another column perfectly.)

But when I ran this line :

df['Number of Pages'] = df['Number of Pages'].apply(lambda x : x.split(' ')[0])

It gives this error :

AttributeError: 'int' object has no attribute 'split'

but the column data type is object so what exactly is going on ?

TIA

1 Answers

If values are not strings but integers, error is not raised by is return missing value:

df['Number of Pages'] = df['Number of Pages'].str.split().str[0]
Related