You need to add a parameter errors='coerce'
to the function to_numeric
:
ID = pd.to_numeric(ID, errors='coerce')
If ID
- column:
df.ID = pd.to_numeric(df.ID, errors='coerce')
NaN
, float
.
int
NaN
, . 0
, int
:
df.ID = pd.to_numeric(df.ID, errors='coerce').fillna(0).astype(np.int64)
:
df = pd.DataFrame({'ID':['4806105017087','4806105017087','CN414149']})
print (df)
ID
0 4806105017087
1 4806105017087
2 CN414149
print (pd.to_numeric(df.ID, errors='coerce'))
0 4.806105e+12
1 4.806105e+12
2 NaN
Name: ID, dtype: float64
df.ID = pd.to_numeric(df.ID, errors='coerce').fillna(0).astype(np.int64)
print (df)
ID
0 4806105017087
1 4806105017087
2 0