转换为数字python时出错

时间:2018-06-11 15:27:57

标签: pandas reshape data-cleaning

我在数据框中有一个列,其中包含一些数字和文本

2009-07-06 05:06:09     Comm Fail
2009-07-06 05:06:29      48,15125
2009-07-06 07:54:58      48,03313
2009-07-06 10:27:29      48,16438
2009-07-07 01:02:19      48,15125
2009-07-07 03:18:28      48,15125
2009-07-07 03:18:38  Scan Timeout
2009-07-07 03:20:19       48,1425
2009-07-07 08:57:49       48,1775
2009-07-07 11:15:38      48,16875
2009-07-08 05:04:59      48,15125
2009-07-08 08:34:39     Comm Fail


 dtype: object

但是当我转换为np.float时,我使用:

Frequency['Frequency']=pd.to_numeric(Frequency['Frequency'], errors='coerce')

但我得到了结果:

2009-07-06 05:06:29   NaN
2009-07-06 07:54:58   NaN
2009-07-06 10:27:29   NaN
2009-07-07 01:02:19   NaN
2009-07-07 03:18:28   NaN
2009-07-07 03:18:38   NaN
2009-07-07 03:20:19   NaN
2009-07-07 08:57:49   NaN
2009-07-07 11:15:38   NaN
2009-07-08 05:04:59   NaN
2009-07-08 08:34:39   NaN
2009-07-08 09:09:08   NaN

我如何解决这个问题..?

1 个答案:

答案 0 :(得分:2)

问题在于逗号。那些小数点是?

df['Frequency'] = (
    pd.to_numeric(df['Frequency'].str.replace(',', '.'), errors='coerce'))

如果它们不是小数点,只需删除它们(改为使用.str.replace(',', ''))。