对数据框列使用apply/map

2024-09-29 06:30:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧:

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

Tags: oftextdfbyversionfullsplitme