Reputation: 75
I'm working with a very simple dataframe. It has just one feature but that feature seems to appear as a float even though it saiys it is a object it gives me error when i try to use it as a string.
Id
0 4853.0
1 4853.0
2 23430.0
3 40704.0
4 23298.0
5 6246.0
6 7345.0
When i do dtypes it devolves:
actor_identity_id float64 dtype: object
So i did:
df['Id'] = df['Id'].astype(str)
But it doesn't do anything because later i try to use it as a string:
if df['Id'].endswith('.0'):
url = df['Id'][:-2]
To get rid of the end of the string and it devolves the error:
AttributeError: 'Series' object has no attribute 'endswith'
It gives the same error when i use rsplit.
Does anyone know what i'm doing wrong?
Upvotes: 0
Views: 131
Reputation: 3967
To point out your problem, currently you're having a Series
and expecting it to have an attribute endswith
with is an attribute of string
. Hence, you should do df['Id'].str.endswith('.0')
.
However, better implement the same using apply
function. Note, when using lambda you take one element at a time and hence no use of str
as written by @U9-Forward.
Upvotes: 1
Reputation: 543
If you are sure that it will always contain .0 at end then instead you could use a simple way
df['id']=df['id'].apply(lambda x: int(x))
Since it is id column and i guess it can't be float value Edit:
If you are not sure then
df['id']=df['id'].apply(lambda x: int(x) if x-int(x)==0)
Upvotes: 1
Reputation: 71560
Or another cleaner way, using apply
:
df['Id']=df['Id'].apply(lambda x: x[:-2] if x.endswith('.0') else x)
Upvotes: 2