Pandas Invalid Literal For Long() With Base 10 Error
I am trying to do: df['Num_Detections'] = df['Num_Detections'].astype(int) And i get following error: ValueError: invalid literal for long() with base 10: '12.0' My data looks
Solution 1:
There is some value, which cannot be converted to int
.
You can use to_numeric
and get NaN
where is problematic value:
df['Num_Detections'] = pd.to_numeric(df['Num_Detections'], errors='coerce')
If need check rows with problematic values, use boolean indexing
with mask with isnull
:
print (df[ pd.to_numeric(df['Num_Detections'], errors='coerce').isnull()])
Sample:
df = pd.DataFrame({'Num_Detections':[1,2,'a1']})
print (df)
Num_Detections
0 1
1 2
2 a1
print (df[ pd.to_numeric(df['Num_Detections'], errors='coerce').isnull()])
Num_Detections
2 a1
df['Num_Detections'] = pd.to_numeric(df['Num_Detections'], errors='coerce')
print (df)
Num_Detections
0 1.0
1 2.0
2 NaN
Post a Comment for "Pandas Invalid Literal For Long() With Base 10 Error"