【问题标题】:ValueError: could not convert string to float: idValueError:无法将字符串转换为浮点数:id
【发布时间】:2025-12-12 17:50:01
【问题描述】:

我正在运行以下 python 脚本:

#!/usr/bin/python

import os,sys
from scipy import stats
import numpy as np

f=open('data2.txt', 'r').readlines()
N=len(f)-1
for i in range(0,N):
    w=f[i].split()
    l1=w[1:8]
    l2=w[8:15]
    list1=[float(x) for x in l1]
    list2=[float(x) for x in l2]
    result=stats.ttest_ind(list1,list2)
    print result[1]

但是我得到了如下错误:

ValueError: could not convert string to float: id

我对此感到困惑。 当我在交互部分中仅对一行尝试此操作时,而不是使用脚本进行循环:

>>> from scipy import stats
>>> import numpy as np
>>> f=open('data2.txt','r').readlines()
>>> w=f[1].split()
>>> l1=w[1:8]
>>> l2=w[8:15]
>>> list1=[float(x) for x in l1]
>>> list1
[5.3209183842, 4.6422726719, 4.3788135547, 5.9299061614, 5.9331108706, 5.0287087832, 4.57...]

效果很好。

谁能解释一下这个? 谢谢。

【问题讨论】:

  • csv 文件中读取类型为df = df[['p']].astype({'p': float}) 的数据帧时,可能会出现这种错误ValueError: could not convert string to float: 。如果csv 是用空格记录的,python 不会将空格字符识别为 nan。您需要使用 df = df.replace(r'^\s*$', np.nan, regex=True) 用 NaN 覆盖空单元格

标签: python string floating-point


【解决方案1】:

显然,您的某些行没有有效的浮点数据,特别是某些行的文本 id 无法转换为浮点数。

当您在交互式提示中尝试时,您只尝试第一行,因此最好的方法是打印出现此错误的行,您将知道错误的行,例如

#!/usr/bin/python

import os,sys
from scipy import stats
import numpy as np

f=open('data2.txt', 'r').readlines()
N=len(f)-1
for i in range(0,N):
    w=f[i].split()
    l1=w[1:8]
    l2=w[8:15]
    try:
        list1=[float(x) for x in l1]
        list2=[float(x) for x in l2]
    except ValueError,e:
        print "error",e,"on line",i
    result=stats.ttest_ind(list1,list2)
    print result[1]

【讨论】:

    【解决方案2】:

    我的错误非常简单:包含数据的文本文件的最后一行有一些 空格(因此不可见)字符。

    作为 grep 的输出,我得到了 45  而不仅仅是 45

    【讨论】:

    • 空格和制表符可见 ;) 行尾和类似字符不是字符\n,\r
    • 我想这是大多数人发现Lib/re.py 和 .replace(' ', '') 存在的时间点。
    【解决方案3】:

    这个错误非常冗长:

    ValueError: could not convert string to float: id
    

    某处在你的文本文件中,一行包含id这个词,它不能真正转换为数字。

    您的测试代码有效,因为 id 一词不在 line 2 中。


    如果你想抓住那条线,试试这段代码。我稍微清理了你的代码:

    #!/usr/bin/python
    
    import os, sys
    from scipy import stats
    import numpy as np
    
    for index, line in enumerate(open('data2.txt', 'r').readlines()):
        w = line.split(' ')
        l1 = w[1:8]
        l2 = w[8:15]
    
        try:
            list1 = map(float, l1)
            list2 = map(float, l2)
        except ValueError:
            print 'Line {i} is corrupt!'.format(i = index)'
            break
    
        result = stats.ttest_ind(list1, list2)
        print result[1]
    

    【讨论】:

      【解决方案4】:

      对于带有逗号数字列的 Pandas 数据框,请使用:

      df["Numbers"] = [float(str(i).replace(",", "")) for i in df["Numbers"]]
      

      所以像4,200.42 这样的值将被转换为4200.42 作为浮点数。

      奖励 1:这是 快速

      奖励 2:如果以 Apache Parquet 格式保存该数据帧,则空间效率更高。

      【讨论】:

        【解决方案5】:

        也许你的数字实际上不是数字,而是伪装成数字的字母?

        就我而言,我使用的字体意味着“l”和“1”看起来非常相似。我有一个像'l1919'这样的字符串,我认为它是'11919',这把事情搞砸了。

        【讨论】:

          【解决方案6】:

          您的数据可能不是您所期望的——您似乎在期待,但没有得到,浮动。

          找出发生这种情况的一个简单解决方案是在 for 循环中添加一个 try/except :

          for i in range(0,N):
              w=f[i].split()
              l1=w[1:8]
              l2=w[8:15]
              try:
                list1=[float(x) for x in l1]
                list2=[float(x) for x in l2]
              except ValueError, e:
                # report the error in some way that is helpful -- maybe print out i
              result=stats.ttest_ind(list1,list2)
              print result[1]
          

          【讨论】:

            【解决方案7】:

            最短路径:

            df["id"] = df['id'].str.replace(',', '').astype(float) - 如果 ',' 是问题

            df["id"] = df['id'].str.replace(' ', '').astype(float) - 如果空格是问题

            【讨论】:

              【解决方案8】:

              我用 pandas 的基本技术解决了类似的情况。首先使用pandas加载csv或者文本文件,很简单

              data=pd.read_excel('link to the file')
              

              然后将数据的索引设置为需要更改的尊重列。例如,如果您的数据将 ID 作为一个属性或列,则将索引设置为 ID。

               data = data.set_index("ID")
              

              然后使用以下命令删除所有以“id”为值而不是数字的行。

                data = data.drop("id", axis=0). 
              

              希望对你有所帮助。

              【讨论】:

                【解决方案9】:

                用 0.0 值更新空字符串值: 如果您知道可能的非浮点值,请更新它。

                df.loc[df['score'] == '', 'score'] = 0.0
                
                
                df['score']=df['score'].astype(float)
                

                【讨论】: