【问题标题】:Invalid literal with base 10以 10 为底的无效文字
【发布时间】:2013-04-16 21:05:31
【问题描述】:

我有一个从 'a' 下面的 csv 构造的 df 应该是双精度数。如果我检查一个?它似乎是一个字符串。在尝试将其转换为 int(a) 时,我收到一个类型错误, 我不知道为什么。

   ValueError: invalid literal for int() with base 10: '46.074'

  a=df["MA10"].ix[100]
   a=int(a) 


 Imported:
 df=pd.read_csv('__.csv',header=None,parse_dates=True, index_col={0},names="__".split())

【问题讨论】:

    标签: python-2.7 casting pandas


    【解决方案1】:

    使用浮动

    In [14]: int('46.074')
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-14-f8e7b101e6bd> in <module>()
    ----> 1 int('46.074')
    
    ValueError: invalid literal for int() with base 10: '46.074'
    
    In [15]: float('46.074')
    Out[15]: 46.074
    

    【讨论】:

    • 谢谢。考虑一下我正在使用 iterrows() 将其转换为字符串的事实吗?它们在数据框中被视为双打。 IE。我正在使用 idx,row in df.iterrows()
    • 显示你的框架在读入后的样子并显示一个 df.get_dtype_counts()。你的最终目标是什么,你用 iterrows 做什么?
    • 我看到了 float 4,object 10。我的最终目标在这里:stackoverflow.com/questions/16019275/…
    • 我在那里链接到我的脚本,但我认为 deque 可能会更好地实现这个目标......
    • 读入后检查你的数据,如果一列中有非数字应该是浮动的,那么它将是对象并且不起作用。您可以尝试df.convert_object(convert_numeric=True) 使用 0.11.0rc1 强制转换为数字(或设置为 NaN)
    【解决方案2】:

    试试这个

        int(float('46.074')//1)
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 2020-01-04
      • 2010-12-22
      • 2011-07-07
      相关资源
      最近更新 更多