【问题标题】:Pandas, object column to string [duplicate]熊猫,对象列到字符串[重复]
【发布时间】:2020-12-21 21:52:35
【问题描述】:

我的熊猫 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

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

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

Pandas API 略有变化。试试看:

df = df.astype("string")

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 2020-04-08
  • 2020-04-09
  • 2018-10-31
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多