Issue
Hope someone can help me I have
Number | Object |
---|---|
Name1 | main |
Name2 | 490348,0 |
Name3 | 237928,0 |
df type is object für column "Object" I need to get rid of the float numbers and turn them into int numbers. But I get an error "cannot convert float NaN to integer".
Thanks a lot!!!
Solution
You can try via pd.to_numeric()
+np.where()
:
import numpy as np
s=pd.to_numeric(df['Object'].replace(',','',regex=True),errors='coerce')
df['Object']=np.where(s.notna(),s.fillna(0).astype(int),df['Object'])
output of df
:
Number Object
0 Name1 main
1 Name2 490348
2 Name3 237928
Now you can check the dtype of df['Object']
:
df['Object'].map(type)
#output:
0 <class 'str'>
1 <class 'int'>
2 <class 'int'>
Name: Object, dtype: object
Note: It is not a good idea to have a column of different dtypes
Answered By - Anurag Dabas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.