Pandas&python类型错误:“float”类型的对象没有len()

2024-05-14 13:48:52 发布

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

我的数据集如下所示:

    film_title      writers        actors

0                                  Leonardo Dicaprio, Jason Statham, Dwayne Johnson...
1                                  Jack Nicholson, Robert De Niro, Denzel Washington...
2                                  Jack Nicholson, Jason Statham, Dwayne Johnson...

“…”表示该单元中有更多的参与者;我试图把所有的演员都列在一个列表中(不包括重复的演员)。到目前为止,我有以下代码:

actorsList = df_final.actors.str.split(', ') #which splits the cells into multiple lists

#print(actorsList) will print this:
['Leonardo Dicaprio', 'Jason Statham', 'Dwayne Johnson'...]
['Jack Nicholson', 'Robert De Niro', 'Denzel Washington'...]
['Jack Nicholson', 'Jason Statham', 'Dwayne Johnson'...]

所以

print(actorsList[0]) #will print the first list: ['Leonardo Dicaprio', 'Jason Statham', 'Dwayne Johnson'...]

然后我尝试再次遍历这个列表,并存储每个演员的名字(不是重复的,因为它们可以出现在多部电影中)

#ITERATE THROUGH ONE LIST
for i in range(len(actorsList[0])):
    txt = actorsList[0][i].split(', ')
    print(txt)

这张照片是这样的:

['Leonardo Dicaprio']
['Jason Statham']
['Dwayne Johnson']
and so on

我尝试对每个列表执行此操作,但是,我最终得到以下错误:

       23 for i in range(len(actorsList)-1):
  ---> 24     for j in range(len(actorsList[i])):
       25         txt = actorsList[i][j].split(', ')
       26         print(txt)

       TypeError: object of type 'float' has no len()

我还应该提到它运行(打印结果)的事实,然而,它停止了,然后我得到了这个错误


Tags: txt列表lenleonardosplitprintjackjason

热门问题