pandas long() 的无效文字,基数为 10 错误
问题:pandas long() 的无效文字,基数为 10 错误 我正在尝试做:df['Num_Detections'] = df['Num_Detections'].astype(int) 我收到以下错误: ValueError: 以 10 为基数的 long() 的无效文字:'12.0' 我的数据看起来如下: >>> df['Num_Detections'].head() Out[6]: sk
·
问题:pandas long() 的无效文字,基数为 10 错误
我正在尝试做:df['Num_Detections'] = df['Num_Detections'].astype(int)
我收到以下错误:
ValueError: 以 10 为基数的 long() 的无效文字:'12.0'
我的数据看起来如下:
>>> df['Num_Detections'].head()
Out[6]:
sku_name
DOBRIY MORS GRAPE-CRANBERRY-RASBERRY 1L 12.0
AQUAMINERALE 5.0L 9.0
DOBRIY PINEAPPLE 1.5L 2.0
FRUKT.SAD APPLE 0.95L 154.0
DOBRIY PEACH-APPLE 0.33L 71.0
Name: Num_Detections, dtype: object
知道如何正确进行转换吗?
感谢帮助。
解答
有一些值,不能转换成int
。
您可以使用to_numeric
并获取NaN
有问题的值:
df['Num_Detections'] = pd.to_numeric(df['Num_Detections'], errors='coerce')
如果需要检查具有问题值的行,请使用boolean indexing
和带有isnull
的掩码:
print (df[ pd.to_numeric(df['Num_Detections'], errors='coerce').isnull()])
样本:
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
更多推荐
目录
所有评论(0)