【问题标题】:Pandas DataFrame Error: ValueError: StringArray requires a sequence of strings or pandas.NAPandas DataFrame 错误:ValueError:StringArray 需要一系列字符串或 pandas.NA
【发布时间】:2020-11-19 15:59:23
【问题描述】:

尝试将 kaggle titanic 数据集 pclass 列数据类型从 int 转换为 string。 使用熊猫 1.0.5。

df.Pclass.head()
0    3
1    1
2    3
3    1
4    3
Name: Pclass, dtype: int64

代码如下:

df['Pclass'] = df['Pclass'].astype('string')

抛出的错误如下:

ValueError: StringArray requires a sequence of strings or pandas.NA

注意:这是第一次工作,不确定从第二次开始会一直收到此错误。

【问题讨论】:

    标签: python-3.x string types int


    【解决方案1】:

    以下代码有效。由于 pandas >= 1.0,他们建议使用字符串而不是 str。但是如果 source 是数字,那么我们可以使用 str 而不是 string 来工作。

    df['Pclass'] = df['Pclass'].astype('str')
    

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 2016-10-04
      • 1970-01-01
      • 2020-12-23
      • 2021-05-12
      • 1970-01-01
      • 2019-02-25
      • 2021-05-18
      • 2017-05-14
      相关资源
      最近更新 更多