Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 将列从str转换为float_Python_Python 3.x_Pandas - Fatal编程技术网

Python 将列从str转换为float

Python 将列从str转换为float,python,python-3.x,pandas,Python,Python 3.x,Pandas,对于下面的数据帧,我使用代码 df['%'] = ((df['Code Lines'] / df['Code Lines'].sum()) * 100).round(2).astype(str) + '%' 输出 Language # of Files Blank Lines Comment Lines Code Lines % C++ 15 66 35 354 6.13% C/C++

对于下面的数据帧,我使用代码

df['%'] = ((df['Code Lines'] / df['Code Lines'].sum()) * 100).round(2).astype(str) + '%'
输出

Language    # of Files  Blank Lines Comment Lines   Code Lines  % 
C++              15          66           35            354    6.13%
C/C++ Header      1           3            7              4    0.07%
Markdown          6           73           0            142    2.46%
Python           110         1998       2086           4982    86.27%
Tcl/Tk            1          14           18            273    4.73%
YAML              1           0            6             20    0.35%
我正在尝试将str转换为float

df['%'] = df['% of Total (Code Only)'].astype('float64')
获取错误

文件 “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/pandas/core/dtypes/cast.py”, 第730行,astype_nansafe 返回arr.astype(dtype,copy=True)值错误:无法将字符串转换为浮点:“0.35%”


是否有办法将列%与符号%一起保持为浮动%

使用
str[:-1]
删除最后一个值(
%
):

但如果可能,更好的方法是:

df['%'] = ((df['Code Lines'] / df['Code Lines'].sum()) * 100).round(2)


另一种方法是使用
条带

df['%'] = df['%'].str.strip('%').astype('float64')

0     6.13
1     0.07
2     2.46
3    86.27
4     4.73
5     0.35
Name: %, dtype: float64

您可以从字符串中删除最后一个字符,如下所示:

str[:-1]
删除最后一个字符

df['%']=df['%'].str[:-1].astype('float64')

或者可以使用replace()将
%
替换为空白字符


df['%']=df['%'].替换(“%”,“”)。astype('float64')

如果保留
%
符号,它将不会是浮点。但是,如果您想删除它,则很容易将其删除。您只需使用
df['%']=((df['code line']/df['code line'].sum())*100)停止即可。第二轮(2)
print (df)
       Language  # of Files  Blank  Lines Comment  Lines Code Lines      %
0           C++          15     66             35               354   6.13
1  C/C++ Header           1      3              7                 4   0.07
2      Markdown           6     73              0               142   2.46
3        Python         110   1998           2086              4982  86.27
4        Tcl/Tk           1     14             18               273   4.73
5          YAML           1      0              6                20   0.35
df['%'] = df['%'].str.strip('%').astype('float64')

0     6.13
1     0.07
2     2.46
3    86.27
4     4.73
5     0.35
Name: %, dtype: float64