python - 如何在熊猫数据框中过滤小写的行和单词?
问题描述
您好我想知道如何在以下数据框中选择包含小写字母的行:
ID Name Note
1 Fin there IS A dog outside
2 Mik NOTHING TO DECLARE
3 Lau no house
我想做的是过滤Note列至少包含一个小写单词的行:
ID Name Note
1 Fin there IS A dog outside
3 Lau no house
并在列表中收集所有小写单词:my_list=['there','dog','outside','no','house']
我试图过滤行是:
df1=df['Note'].str.lower()
对于在列表中附加单词,我认为我应该首先标记字符串,然后选择小写的所有术语。我对吗?
解决方案
用于Series.str.contains
过滤以下中的至少一个小写字符boolean indexing
:
df1 = df[df['Note'].str.contains(r'[a-z]')]
print (df1)
ID Name Note
0 1 Fin there IS A dog outside
2 3 Lau no house
然后Series.str.extractall
提取小写单词:
my_list = df1['Note'].str.extractall(r'(\b[a-z]+\b)')[0].tolist()
print (my_list)
['there', 'dog', 'outside', 'no', 'house']
或者使用拆分句子的列表理解并过滤islower
:
my_list = [y for x in df1['Note'] for y in x.split() if y.islower()]
print (my_list)
['there', 'dog', 'outside', 'no', 'house']
推荐阅读
- javascript - 获取 HTML 数据的加载
- c - 我对此有疑问如何在c中的数组中写入多个字符,请给我一个解决方案
- python - Python:如何过滤每个类别的最新日期行,并从最旧日期的行到最新日期的行填写值
- mysql - 如何在 Django 中按不同的字段值进行过滤?由于 Mysql 数据库,无法使用 distinct() 函数
- python-3.x - 将 python 嵌套字典导出到 CSV 是否有更短的方法?
- python - 更改默认python后netplan无法正常工作
- python - 如何通过 selenium、python 在 hk.jobdb.com 上上传文件?
- javascript - 你如何使用javascipt在页面加载时在检查状态中制作一些复选框,而不是jQuery?
- docker - 发布到 Slack 时出错 - Docker jenkins 找不到有效证书
- node.js - 自动运行 SSH 命令