【问题标题】:I am getting valueError when trying to convert word2number module in pandas尝试在 pandas 中转换 word2number 模块时出现 valueError
【发布时间】:2020-11-02 20:35:58
【问题描述】:

我已经写了下面的代码,但它给了我这个error"ValueError: Type of input is not string! 请输入一个有效的数字字(例如'200万23049')"

df.experience = df.experience.apply(w2n.word_to_num)

【问题讨论】:

  • 我的数据集有像“零、一、二”这样的数据我想要像“1、2、3”这样的数据
  • 问题可能出在您的数据集中。它是如何准确表示的?请编辑您的帖子。以下代码对我来说很好用:df = pd.DataFrame({"experience":['five', 'two', 'seven']}) df.experience = df.experience.apply(w2n.word_to_num) 所以问题不在你的应用行
  • 是的。这个问题在我的数据集中,我解决了。谢谢你的回答。

标签: python-3.x pandas jupyter-notebook


【解决方案1】:

首先使用fillna 填充缺失值(null(Na) 或 Nan)),然后使用您的代码apply

df.experience = df.experience.apply(w2n.word_to_num)

【讨论】:

    【解决方案2】:
    df['experience'] = df['experience'].fillna('zero')
    df.experience = df.experience.apply(w2n.word_to_num)
    df
    

    首先,将(Nan 或 Na)填入“零”而不是 0。 然后尝试运行此代码。

    【讨论】:

      【解决方案3】:

      首先确保该列是 string 类型,否则使用将其转换为字符串

      df["column_name"] = df.column_name.astype(str)
      

      然后尝试申请

      df["column_name"] = df.column_name.apply(word_to_num)
      

      【讨论】:

        猜你喜欢
        • 2020-03-29
        • 1970-01-01
        • 2019-10-27
        • 2021-09-14
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 1970-01-01
        • 2017-01-09
        相关资源
        最近更新 更多