python - Pandas:为元素列表的数据框赋值(如果存在)
问题描述
我正在尝试从列表中的元素分配值,如果它是startswith
这个子字符串到熊猫数据框列
代码:
searchwords = ['harry','harry potter','lotr','secret garden']
l1 = [1, 2, 3,4,5]
l2 = ['Harry Potter is a great book',
'Harry Potter is very famous',
'I enjoyed reading Harry Potter series',
'LOTR is also a great book along',
'Have you read Secret Garden as well?'
]
df = pd.DataFrame({'id':l1,'text':l2})
df['text'] = df['text'].str.lower()
数据预览:
id text
0 1 harry potter is a great book
1 2 harry potter is very famous
2 3 i enjoyed reading harry potter series
3 4 lotr is also a great book along
4 5 have you read secret garden as well?
试过:
df.loc[df['text'].str.startswith(tuple(searchwords)),'tags'] if (df['text'].str.startswith(tuple(searchwords))) == True else np.NaN
错误:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我做错了什么?== True
我认为您可以在 if/else 逻辑中分配值
寻找这样的输出:
id text tags
0 1 harry potter is a great book harry;harry potter
1 2 harry potter is very famous haryy;harry potter
2 3 i enjoyed reading harry potter series NaN
3 4 lotr is also a great book along lotr
4 5 have you read secret garden as well? NaN
解决方案
推荐阅读
- html - 为什么嵌入式 SVG 会导致桌面上的 CLS(累积布局移位)而不是移动设备上的?
- java - 如何避免在 JTextPane 内换行/换行
- mongodb - 在 Elasticsearch 中为结果分数添加条件
- ubuntu-18.04 - 如何在 Ubuntu 18.04 服务器上默认启用 SMB1 并禁用 SMB2 和 SMB3?
- python - Anaconda 没有更新 Spyder
- php - 安装失败,将 ./composer.json 还原为其原始内容 [Composer\Repository\InvalidRepositoryException]
- flutter - WillPopScope 不适用于不同选项卡中存在的不同 WebView
- java - 当对象 A 包含对对象 B 的引用时,是否可以使用 Mapstruct 将对象 B 的属性映射到对象 A_DTO 的属性?
- batch-file - 在批处理文件中显示复制过程的错误消息
- typescript - 动态样式表属性访问的类型问题