You need add parameter errors='coerce' to function to_numeric:

CopyID = pd.to_numeric(ID, errors='coerce')

If ID is column:

Copydf.ID = pd.to_numeric(df.ID, errors='coerce')

but non numeric are converted to NaN, so all values are float.

For int need convert NaN to some value e.g. 0 and then cast to int:

Copydf.ID = pd.to_numeric(df.ID, errors='coerce').fillna(0).astype(np.int64)

Sample:

Copydf = 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

EDIT: If use pandas 0.25+ then is possible use integer_na:

Copydf.ID = pd.to_numeric(df.ID, errors='coerce').astype('Int64')
print (df)
              ID
0  4806105017087
1  4806105017087
2            NaN
Answer from jezrael on Stack Overflow
Top answer
1 of 3
123

You need add parameter errors='coerce' to function to_numeric:

CopyID = pd.to_numeric(ID, errors='coerce')

If ID is column:

Copydf.ID = pd.to_numeric(df.ID, errors='coerce')

but non numeric are converted to NaN, so all values are float.

For int need convert NaN to some value e.g. 0 and then cast to int:

Copydf.ID = pd.to_numeric(df.ID, errors='coerce').fillna(0).astype(np.int64)

Sample:

Copydf = 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

EDIT: If use pandas 0.25+ then is possible use integer_na:

Copydf.ID = pd.to_numeric(df.ID, errors='coerce').astype('Int64')
print (df)
              ID
0  4806105017087
1  4806105017087
2            NaN
2 of 3
10
  1. If you're here because you got
OverflowError: Python int too large to convert to C long

use .astype('int64') for 64-bit signed integers:

Copydf['ID'] = df['ID'].astype('int64')

If you don't want to lose the values with letters in them, use str.replace() with a regex pattern to remove the non-digit characters.

Copydf['ID'] = df['ID'].str.replace('[^0-9]', '', regex=True).astype('int64')

Then input

0    4806105017087
1    4806105017087
2         CN414149
Name: ID, dtype: object

converts into

0    4806105017087
1    4806105017087
2           414149
Name: ID, dtype: int64
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-string-to-integer-in-pandas-dataframe
How to Convert String to Integer in Pandas DataFrame? - GeeksforGeeks
July 15, 2025 - Interview Questions · Examples ... · Let's see methods to convert string to an integer in Pandas DataFrame: Method 1: Use of Series.astype() method....
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert string to integer
Pandas Convert String to Integer - Spark By {Examples}
June 3, 2025 - To convert a string column to integers in a Pandas DataFrame, you can use either the astype(int) method or the pd.to_numeric() function. This conversion changes the column’s data type from object (which is how strings are typically stored ...
🌐
Saturn Cloud
saturncloud.io › blog › pandas-convert-string-to-int-a-guide-for-data-scientists
Pandas Convert String to Int A Guide for Data Scientists | Saturn Cloud Blog
December 21, 2023 - In this example, we have used replace and fillna() to replace missing values with ‘0’ before converting to integers. If the ‘numbers’ column contains non-numeric strings, such as ‘NaN’ or ‘None’, the astype() method will raise ...
🌐
Medium
datadiscoveries.medium.com › how-to-convert-strings-to-integers-in-pandas-dataframe-b35b6a88c6cd
How to Convert Strings to Integers in Pandas DataFrame | by Olaide Kashimawo | Medium
August 4, 2022 - How to Convert Strings to Integers in Pandas DataFrame #string #integer # beginners # convert The purpose of this blog is to converting string into integer in pandas dataframe when conducting data …
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-string-to-integer-in-pandas-dataframe
How to Convert String to Integer in Pandas DataFrame?
It's important to note that when ... are any non-numeric characters or missing values in the column, this method will not work as intended. The to_numeric() function in Pandas is another useful method for converting string columns to integer data type....
Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_numeric.html
pandas.to_numeric — pandas 3.0.1 documentation
Convert dtypes. ... >>> s = pd.Series(["1.0", "2", -3]) >>> pd.to_numeric(s) 0 1.0 1 2.0 2 -3.0 dtype: float64 >>> pd.to_numeric(s, downcast="float") 0 1.0 1 2.0 2 -3.0 dtype: float32 >>> pd.to_numeric(s, downcast="signed") 0 1 1 2 2 -3 dtype: int8 >>> s = pd.Series(["apple", "1.0", "2", -3]) >>> pd.to_numeric(s, errors="coerce") 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64
🌐
TutorialsPoint
tutorialspoint.com › fastest-way-to-convert-integers-to-strings-in-pandas-dataframe
Fastest way to Convert Integers to Strings in Pandas DataFrame
In the below example, we create a sample data frame with an integer column and then define a lambda function to convert integer to string and apply that lambada function to each element of the column. import pandas as pd # create a sample data frame with an integer column df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]}) # define a lambda function to convert integers to strings int_to_str = lambda x: str(x) # apply the lambda function to the integer column df['int_column'] = df['int_column'].apply(int_to_str) # print the data frame print(df)
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-integers-to-strings-in-pandas-dataframe
How to Convert Integers to Strings in Pandas DataFrame? - GeeksforGeeks
July 1, 2022 - In Python, a string can be converted into an integer using the following methods : Method 1: Using built-in int() function: If your string contains a decimal integer and you wish to convert it into an int, in that case, pass your string to int() ...
🌐
Wellsr
wellsr.com › python › python-convert-pandas-dataframe-string-to-float-int
Python with Pandas: Convert String to Float and other Numeric Types - wellsr.com
November 9, 2018 - This tutorial will show you how to convert Pandas DataFrame strings into floats or ints. Converting Pandas string data to numeric types is required before performing numeric calculations.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-integer-in-python
Convert String to Int in Python - GeeksforGeeks
The simplest way to convert a string to an integer in Python is by using the int() function.
Published   September 11, 2025
🌐
Kanaries
docs.kanaries.net › topics › Python › covnert-string-to-int-python
How to Convert String to Int in Python: Easy Guide – Kanaries
In this essay, we will explore type casting, built-in functions, and error-handling techniques to effectively convert strings to integers in Python. Want to quickly create Data Visualization from Python Pandas Dataframe with No code?
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert column to int in dataframe
Pandas Convert Column to Int in DataFrame - Spark By {Examples}
June 26, 2025 - You can also use DataFrame.apply() method to convert Fee column from string to integer in pandas. As you see in this example we are using numpy.int64.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › convert-a-dataframe-column-to-integer-in-pandas
Convert a Dataframe Column to Integer in Pandas - GeeksforGeeks
July 23, 2025 - It is best when you need to apply a straightforward, element-wise transformation to each value in the column, such as converting string values to integers. ... import pandas as pd # Creating a sample DataFrame data = {'Column1': ['1', '2', '3', '4']} df = pd.DataFrame(data) # Using map() method to convert to integers df['Column1'] = df['Column1'].map(int) # Displaying the DataFrame and dtype print(df) print(df['Column1'].dtype)
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.convert_dtypes.html
pandas.DataFrame.convert_dtypes — pandas 3.0.1 documentation
>>> dfn = df.convert_dtypes() >>> dfn a b c d e f 0 1 x True h 10 <NA> 1 2 y False i <NA> 100.5 2 3 z <NA> <NA> 20 200.0 · >>> dfn.dtypes a Int32 b string c boolean d string e Int64 f Float64 dtype: object
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.17.0 › generated › pandas.to_numeric.html
pandas.to_numeric — pandas 0.17.0 documentation
Convert argument to a numeric type. ... >>> import pandas as pd >>> s = pd.Series(['1.0', '2', -3]) >>> pd.to_numeric(s) >>> s = pd.Series(['apple', '1.0', '2', -3]) >>> pd.to_numeric(s, errors='ignore') >>> pd.to_numeric(s, errors='coerce')
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert integer to string in dataframe
Pandas Convert Integer to String in DataFrame - Spark By {Examples}
December 5, 2024 - A lambda function is provided to ... a Pandas Series to strings using the map(str) method, you can apply the str function to each element in the Series....