Python中和Pandas中数据类型对应关系如下:
- 如果数据是纯净的数据,可以转化为数字
- astype基本也就是两种用作,数字转化为单纯字符串,单纯数字的字符串转化为数字,含有其他的非数字的字符串是不能通过astype进行转化的。
- 需要引入其他的方法进行转化,也就有了下面的自定义函数方法
自定义函数进行转化
replace()方法去掉字符串里面的一些不想要的东西
def convert_currency(var):
"""
convert the string number to a float
_ 去除$
- 去除逗号,
- 转化为浮点数类型
"""
new_value = var.replace(",","").replace("$","")
return float(new_value)
详细见:https://www.cnblogs.com/onemorepoint/p/9404753.html