【发布时间】:2021-05-25 08:42:44
【问题描述】:
我有一个数据框,该数据框当前包含一列存储为字符串(对象)的收入数据。但是,当我使用时,我想将其转换为浮动:
df['Income'] = pd.to_numeric(df['Income_2016'], errors='coerce')
并使用 df.dtypes,Income 是 float64,但是当我显示 df 时,以前的 str 数字值现在变成了 NaN。
转换前:
| Index | Income |
|---|---|
| 1 | 100,516 |
| 2 | 101,551 |
转换后:
| Index | Income |
|---|---|
| 1 | NaN |
| 2 | NaN |
如何在不将原始值转换为 NaN 的情况下将其转换为浮点数?
【问题讨论】:
-
用点替换逗号