熊猫,对象列到字符串

时间:2020-09-02 17:09:59

标签: python pandas dataframe

我的熊猫df:

      first   second    third fourth fifth
0       x       y        12     2      0
1       a       b         1     1      1
2       c       d         2    12      0

要检查列数据类型,请使用:

df.info()

是结果:

<class 'pandas.core.frame.DataFrame'>
first    380 non-null object
second   380 non-null object
third    380 non-null object
fourth   380 non-null object
fifth    380 non-null int32

我想将第一列和第二列转换为字符串:

df['first'] = df['first'].astype(str)
df['second'] = df['second'].astype(str)

我要检查:

df.info()

first    380 non-null object
second   380 non-null object
third    380 non-null object
fourth   380 non-null object
fifth    380 non-null int32

所以它不起作用,我在哪里弄错了?

1 个答案:

答案 0 :(得分:-1)

Pandas API略有更改。只需尝试:

df = df.astype("string")