我们可以使用pand
as
中的str.extract()方法来从字符串数据中提取关键词。
具体步骤如下:
首先,将字符串数据转换为Series类型。
然后,使用正则表达式匹配关键词,将其提取出来。
代码示例:
import pand
as
as
pd
创建字符串数据
data = pd.Series(['Python is a popular programming language',
'It is used for web development',
'Data
analysis
and machine learning',
'Python is e
as
y to learn and understand'])
提取关键词
keywo
rds
= data.str.extract('(\w+)', expand=False)
输出关键词
print(keywo
rds
)
结果如下:
0 Python
1 is
2 a
3 popular
4 programming
5 language
6 It
7 is
8 used
9 for
...中间省略...
20 e
as
y
21 to
22 learn
23 and
24 understand
dtype: object
在上述示例代码中,我们使用'\w+'正则表达式来匹配关键词,其中'\w+'表示由一个或多个字母、数字或下划线字符组成的单词。
最后,我们使用expand=False参数,以Series类型的形式返回提取出来的关键词,而不是返回DataFrame类型。