python - Pandas.Series.Str.Find 在列表中与 x 混合
问题描述
下午好!
长话短说,我正在尝试根据评论数据集对手机的某些功能进行情感分析。我正在将它与 .loc 函数协调,它以前工作过,但这是一个特定的列表而不是字符串。我正在尝试将其链接到列表中的任何 x,其中 x 是一个列表。
这是我所拥有的:
Battery = ['battery', 'charge', 'juice', 'talk time', 'hours', 'minutes']
batt = apple['Reviews'].str.lower().str.find(x in Battery)!=-1
返回的错误是:
AttributeError: Can only use .str accessor with string values.
我这样做是因为它不喜欢我只是在电池中放入电池而不是 x。
有什么建议么?再次感谢!
如果我运行分配的变量,预期的输出将是所有具有任何关键字的行。(电池内的 x)。因此,任何带有充电、果汁等内容的行都会弹出。
解决方案
如果apple['Review']
只是一列字符串,您可以检查str.contains()
.
鉴于这些Battery
和apple
:
Battery = ['battery', 'charge', 'juice', 'talk time', 'hours', 'minutes']
apple = pd.DataFrame({'Review': ['abc battery xyz', 'foo bar', 'orange juice bar', 'talk time']})
# Review
# 0 abc battery xyz
# 1 foo bar
# 2 orange juice bar
# 3 talk time
这将是batt
输出:
batt = apple[apple['Review'].str.lower().str.contains('|'.join(Battery))]
# Review
# 0 abc battery xyz
# 2 orange juice bar
# 3 talk time
如果是一列列表,您可以在检查之前apple['Review']
先将它们加入:str.join(' ')
str.contains()
batt = apple[apple['Review'].str.join(' ').str.lower().str.contains('|'.join(Battery))]
推荐阅读
- nodatime - 分区日期时间和即时之间的 Noda 时间比较
- php - 数据未通过 PHP 表单保存在数据库中
- html - 使用浮动元素时自动边距
- javascript - ReactNative FlatList 并不总是显示所有项目并且不可滚动。它将行限制为 initialNumToRender,但没有滚动来渲染更多
- c# - 如何使用 Aspose.Word C# 在 MS Word 中插入图标
- ios - iOS capturePhoto() 仅在 iPad 11" 第三代前置摄像头上无法拍摄最高分辨率的照片
- flutter - _MapStream
>, 列表 >' 不是 'Map 类型的子类型 > - scrapy - Scrapy抓取不抓取任何网址
- python - 如何将 MMAP 文件转换为另一种文件类型?
- swift - 在 10 次迭代时早期收敛 CreateML