Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas-将值转换为类型字符串到浮点_Python_Pandas_String_Datetime - Fatal编程技术网

Python Pandas-将值转换为类型字符串到浮点

Python Pandas-将值转换为类型字符串到浮点,python,pandas,string,datetime,Python,Pandas,String,Datetime,在我的df中,我将一些值设置为dtype(str) 我想把它们转换成float,结果是: x 27.47 13.45 10.45 如何做到这一点?在您的情况下,您可以: df['x'] = df['x'].str.replace(':','.').astype(float) 输出: x 0 27.47 1 13.45 2 10.45 试试这个: df['x'] = df['x'].replace(to_replace=":"

在我的df中,我将一些值设置为
dtype(str)

我想把它们转换成float,结果是:

      x
  27.47
  13.45
  10.45


如何做到这一点?

在您的情况下,您可以:

df['x'] = df['x'].str.replace(':','.').astype(float)
输出:

       x
0  27.47
1  13.45
2  10.45
试试这个:

df['x'] = df['x'].replace(to_replace=":",value=".").astype(float)

但是
27分钟,47秒
不是
27.47
分钟?它可以是“.”,没有问题,我的意思是1分钟=60秒,所以
47秒
.76
分钟,而不是
.47
分钟。名称并不重要,实际上,只是转换
.str
是需要的。不需要。因为这是Pandas.replace()方法好吧,下面的解决方案奏效了,唯一的区别是……是的……我确保通过了
df.x.astype(str)
……但不知怎的,它在替换之前只与.str一起工作。对于我来说,它在没有
.str
的情况下工作,仍然无法找出原因
df['x'] = df['x'].replace(to_replace=":",value=".").astype(float)