我在数据集中有一列“高度”,如下所示。
Height
0 6-2
1 6-6
2 6-5
3 6-5
4 6-10
5 6-9
6 6-8
7 7-0
它的类型是dtype: object
现在我想将它转换为 float ,即6.2、6.6 我尝试使用替换方法,但它不起作用。你能建议我该怎么做吗?我是 Pandas 新手。
最佳答案
使用Series.str.replace
用于替换子字符串并转换为 float :
df['Height'] = df['Height'].str.replace('-','.').astype(float)
或者使用Series.replace
使用 regex=True
替换子字符串:
df['Height'] = df['Height'].replace('-','.', regex=True).astype(float)
关于python - 替换 Pandas 中的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55729179/