【问题标题】:Pandas - df.at functionality error - pythonPandas - df.at 功能错误 - python
【发布时间】:2023-03-28 04:13:02
【问题描述】:

我正在尝试让 pandas 在循环结束时将数据框中的某个单元格写入变量(即字符串)。我不断收到以下错误。 代码:

df1.at[i,'Status']=str(result)

回溯错误:

ValueError: could not convert string to float: 'example'

有没有办法使用“df.at”功能输入非数值?

【问题讨论】:

    标签: python excel pandas dataframe object


    【解决方案1】:

    Status dtype 是float,所以at 分配错误时,您将字符串分配给它。您需要将其转换为 dtype object

    df1['Status'] = df1['Status'].astype(str)
    df1.at[i,'Status'] = str(result)
    

    注意:这样做会将Status 中的所有值转换为字符串。

    【讨论】:

    • 这成功了。打印后检查并在那里。虽然现在我必须弄清楚为什么它没有填充到 excel 中应该是一个快速修复。感谢您的帮助!
    猜你喜欢
    • 2013-01-28
    • 2020-02-27
    • 2017-12-31
    • 2017-01-15
    • 2018-05-05
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多