基于len(列)删除数据帧上的行

2024-04-25 03:34:58 发布

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

我有3个包含英语单词、名称和位置的数据框。对于每个数据帧,我尝试删除长度小于3的所有单词,并使用以下代码:

english_words=english_words[english_words[0].map(len) >= 3]
names=names[names[0].map(len) >= 3]
places=places[places[0].map(len) >=3]

所有数据帧只有一列。 我希望得到的新数据帧只包含长度大于或等于3的字,但我得到的错误如下:

TypeError                                 Traceback (most recent call last)
<ipython-input-21-d16d0eaf3a11> in <module>
----> 1 english_words=english_words[[(len(x) > 5) for x in english_words[0]]]
      2 names=names[names[0].map(len) >= 3]
      3 places=places[places[0].map(len) >=3]

<ipython-input-21-d16d0eaf3a11> in <listcomp>(.0)
----> 1 english_words=english_words[[(len(x) > 5) for x in english_words[0]]]
      2 names=names[names[0].map(len) >= 3]
      3 places=places[places[0].map(len) >=3]

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

Tags: 数据in名称mapforinputlenenglish
1条回答
网友
1楼 · 发布于 2024-04-25 03:34:58

问题已修复,这是更新的代码:

english_words=english_words[[(len(str(x)) >=3) for x in english_words[0]]]
names=names[[(len(str(x)) >=3) for x in names[0]]]
places=places[[(len(str(x)) >=3) for x in places[0]]]

相关问题 更多 >