【问题标题】:How to convert column with dtype as object to string in Pandas Dataframe [duplicate]如何将具有dtype的列作为对象转换为Pandas Dataframe中的字符串[重复]
【发布时间】:2026-01-10 16:50:01
【问题描述】:

当我将 csv 文件读取到 pandas 数据框时,每列都会转换为自己的数据类型。我有一列已转换为对象。我想为此列执行字符串操作,例如拆分值和创建列表。但是这样的操作是不可能的,因为它的 dtype 是 object。谁能告诉我如何将列的所有项目转换为字符串而不是对象?

我尝试了几种方法,但都没有奏效。我使用了 astype、str()、to_string 等。

a=lambda x: str(x).split(',')
df['column'].apply(a)

df['column'].astype(str)

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    不直接回答问题,但可能对其他人有所帮助。

    我有一个名为Volume 的列,其中包含-(无效/NaN)和格式为, 的数字

    df['Volume'] = df['Volume'].astype('str')
    df['Volume'] = df['Volume'].str.replace(',', '')
    df['Volume'] = pd.to_numeric(df['Volume'], errors='coerce')
    

    需要转换为字符串才能应用于str.replace

    pandas.Series.str.replace
    pandas.to_numeric

    【讨论】:

      【解决方案2】:

      您可以尝试使用df['column'].str.,然后使用任何字符串函数。 Pandas 文档包括split

      【讨论】:

      • 不,pandas 将存储指向字符串的指针,最后的列类型将是 'object'
      • 我相信 pandas 将始终将字符串列存储为对象
      【解决方案3】:

      由于字符串数据类型具有可变长度,因此默认存储为对象 dtype。如果你想将它们存储为字符串类型,你可以这样做。

      df['column'] = df['column'].astype('|S80') #where the max length is set at 80 bytes,
      

      或者

      df['column'] = df['column'].astype('|S') # which will by default set the length to the max len it encounters
      

      【讨论】:

      • 您使用的是哪个 python 版本?它对我不起作用
      • 得到了TypeError: data type "bytes256" not understood,有什么建议吗?
      • 由于pandas几乎继承了numpy的整个类型系统(除了category)请参考docs.scipy.org/doc/numpy/reference/…了解更多关于类型快捷方式的信息。
      • 在 Python 3.8.2 中工作
      【解决方案4】:

      您是否尝试将其分配回列?

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

      参考这个question,pandas 数据框存储指向字符串的指针,因此它是类型 '目的'。根据docs,你可以试试:

      df['column_new'] = df['column'].str.split(',') 
      

      【讨论】:

      • 是的,我试过了。即使在尝试之后,该列的数据类型仍然是对象。
      • 您能粘贴一个数据框的样本吗?
      • 我已经编辑了答案,请检查它是否有效
      • 它们都不起作用:(