【发布时间】:2018-05-31 09:54:45
【问题描述】:
我有一个数据框,它有两列,“标题”和“描述”。标题栏有一堆与临床实验室测试相关的标题。不幸的是,大多数标题都是相同测试的重复,但由于标题的微小变化,标题显示为唯一。
values = [('Complete blood picture', 'AB'), ('Complete BLOOD test', 'AB'), ('blood glucose', 'AB'), ('COMplete blood Profile', 'AB')]
labels = ['title', 'description']
import pandas as pd
labtest = pd.DataFrame.from_records(values, columns = labels) # Create data frame
labtest = labtest.apply(lambda x: x.astype(str).str.lower()) # Convert columns to string and lower case
labtest['title'].str.contains("blood") # Search for blood
之前:
Title Description
Complete blood test AB
COMPLETE Blood test\ AB
Blood glucose AB
Complete blood picture AB
之后:[这就是我希望数据框的样子]
Title Description
Blood test AB
Blood test AB
Blood test AB
Blood test AB
我想在每个标题中搜索“血液”这个词,如果它是真的,那么将整个标题更改为“血液测试”。
P.S 我是 python 新手并使用文本数据,我只设法搜索“血”这个词。
【问题讨论】:
-
@furas,错字-> str.replace()
-
在你的例子中,“平均血糖(abg)”总是整个字符串吗?或者您想保留开头或结尾的其他文本?
-
@VISWESWARANNAGASIVAM 谢谢。编辑评论太晚了,所以我写了新的。
-
你最好用熊猫来做这个
-
@KarthikShanmukha 不要使用images