对pandas dataframe列使用apply / map

时间:2015-02-28 21:33:48

标签: python pandas dataframe

我有熊猫的数据框:

df.text[:3]

0    nena shot by me   httptcodcrsfqyvh httpstcokxr...
1           full version of soulless    httptcowfmcyyu
2    when youre having a good day but then get to w...
Name: text, dtype: object

基本上它只是一个推文文本系列。没什么。

text = df.text
text.index

Int64Index([0, 1, 2, ...], dtype='int64')

现在我想在本系列中分割单词。它适用于这个:

df.text.str.split('')

0     [nena shot by me   httptcodcrsfqyvh httpstcokx...
1          [full version of soulless    httptcowfmcyyu]
2     [when youre having a good day but then get to ...

但是ID不适用于apply方法:

df.text.apply(lambda x: x.split(' '))

并抛出异常:AttributeError: 'float' object has no attribute 'split'

我做错了什么以及为什么apply方法将此int索引作为参数?

如果我使用df.text.map(lambda x: x.split(' '))

,则相同

UPD

df[df.text == np.nan].shape
(0, 13)

df.text[:3]

0    nena shot by me   httptcodcrsfqyvh httpstcokxr...
1           full version of soulless    httptcowfmcyyu
2    when youre having a good day but then get to w...

工作得很好:

df.text[:3].map(lambda x: x.split())

0    [nena, shot, by, me, httptcodcrsfqyvh, httpstc...
1        [full, version, of, soulless, httptcowfmcyyu]
2    [when, youre, having, a, good, day, but, then,...
Name: text, dtype: object

0 个答案:

没有答案