简体   繁体   English

astype 不适用于 Pandas 数据框

[英]astype does not work for Pandas dataframe

I have a dataframe called messages where data looks like我有一个名为消息的数据框,其中数据看起来像

message             length  class
hello, Come here      16     A
hi, how are you       15     A 
what is it            10     B
maybe tomorrow        14     A

When i do当我做

messages.dtypes

It shows me它显示了我

class      object
message    object
Length      int64
dtype: object

Then I tried converting message column to string type然后我尝试将消息列转换为字符串类型

messages['message'] = messages['message'].astype(str)
print messages.dtypes

It still shows me它仍然显示我

class      object
message    object
Length      int64
dtype: object

What am I doing wrong.我究竟做错了什么。 Why doesn't it convert to string?为什么它不转换为字符串?

Python version 2.7.9 On windows 10 Windows 10 上的 Python 版本 2.7.9
Pandas version 0.15.2熊猫版本 0.15.2

There is no "string" datatype.没有“字符串”数据类型。 In pandas, strings are stored as objects.在 Pandas 中,字符串被存储为对象。

In numpy, you can have string datatypes, but they're fixed-length, so there's still no "string datatype".在 numpy 中,您可以拥有字符串数据类型,但它们是固定长度的,因此仍然没有“字符串数据类型”。 There's a datatype for 5-character strings, a datatype for 10-character strings, etc., but no datatype for "strings" per se.有 5 个字符的字符串的数据类型,10 个字符的字符串的数据类型等,但“字符串”本身没有数据类型。 Pandas uses object as the datatype for strings so that you can perform size-changing manipulations on the strings (eg, concatenating them with other strings) without having to recreate the entire column with a new string length. Pandas 使用object作为字符串的数据类型,因此您可以对字符串执行大小更改操作(例如,将它们与其他字符串连接),而无需使用新的字符串长度重新创建整个列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM