2015-11-27 147 views
25

当我读取一个csv文件到pandas数据框时,每列都转换为它自己的数据类型。我有一列已转换为对象。我想为这个列执行字符串操作,比如分割值和创建一个列表。但是,由于它的dtype是对象,所以不可能有这样的操作。任何人都可以让我知道如何将列的所有项目转换为字符串而不是对象?如何将dtype列转换为Pandas Dataframe中的字符串

我尝试了几种方法,但没有任何工作。我用astype,STR(),to_string等

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

df['column'].astype(str) 
+1

你尝试DF [ '列']。astype( 'STR')?请注意,海峡撇号.. –

+6

大熊猫将始终存储字符串为对象[看看这个链接(http://stackoverflow.com/questions/21018654/strings-in-a-dataframe-but-dtype-is-object ) – sushmit

回答

9

你尝试分配回列?

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

参照该question,熊猫数据帧存储指针串,因此它是类型 “对象”的。由于每docs,你可以尝试:

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

是的,我试过。即使在尝试之后,该列的数据类型仍保持为对象。 – user3546523

+0

你能粘贴您的数据帧的样本? –

+0

我编辑了答案,请检查它是否有效 –

6

因为字符串数据类型有可变长度,它是由默认存储为对象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 
+0

其中你使用的是python版本吗?它不适合我 – Learner

0

你可以尝试使用df['column'].str.,然后使用任何字符串函数。熊猫的文件包括那些像split

相关问题