Given the following data frame:
import pandas as pd
df = pd.DataFrame(
{\'A\':[\'A\',\'B\',\'C\',\'D\'],
\'C\':[\'1\',\'12\',\'*\',\'8\']
int() is the Python standard built-in function to convert a string into an integer value. Convert the column to int using int().
For parsing integers instead of floats, you can use the isdigit() function for string objects.
If you run isdigit() after int(), you can filter the data to rows where the value for column C is an integer.
You could use pd.to_numeric
to convert the C
column to numeric values. Passing errors='coerce
' tells pd.to_numeric
to set non-numeric values to NaN
.
import pandas as pd
df = pd.DataFrame(
{'A':['A','B','C','D'],
'C':['1','12','*','8'] })
df['C'] = pd.to_numeric(df['C'], errors='coerce')
print(df)
prints
A C
0 A 1.0
1 B 12.0
2 C NaN
3 D 8.0
Since NaN values are only allowed in columns with floating-point dtype (or object
dtype), the column can not be set to an integer dtype.