首页 > 解决方案 > 我无法将对象类型列转换为字符串

问题描述

Df['column']
xxx345xxxhgf447jfhf576
Djfnfjf5678
0000004444000000
Xxx88xxx888xxx8888xxx88

8

我试过了

Df['column'].astype(str)

Df['column'].astype('str')
Df['column'].astype('|S')

它仍然作为对象 dtype

标签: pythonpandas

解决方案


您需要在转换为后将其分配给列str

Df['column'] = Df['column'].astype('string')
In [49]: df = pd.DataFrame({"column":["xxx345xxxhgf447jfhf576", "Djfnfjf5678", "0000004444000000","Xxx88xxx888xxx8888xxx88"]})

In [50]: df
Out[50]:
                    column
0   xxx345xxxhgf447jfhf576
1              Djfnfjf5678
2         0000004444000000
3  Xxx88xxx888xxx8888xxx88

In [51]: df.dtypes
Out[51]:
column    object
dtype: object

In [52]: df["column"] = df["column"].astype("string")

In [53]: df.dtypes
Out[53]:
column    string
dtype: object

推荐阅读