-1

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!!!

Dilan
  • 25
  • 3
  • Did your query solved? if so then try considering [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) to signal others that the issue is resolved. If not, you can provide feedback so that the answer can be improved (or removed altogether) – Anurag Dabas Aug 14 '21 at 06:25

1 Answers1

0

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

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41