为了账号安全,请及时绑定邮箱和手机立即绑定

如何在熊猫数据框中过滤小写的行和单词?

如何在熊猫数据框中过滤小写的行和单词?

汪汪一只猫 2023-05-16 16:47:59
您好我想知道如何在以下数据框中选择包含小写字母的行:ID     Name   Note1      Fin    there IS A dog outside2      Mik    NOTHING TO DECLARE3      Lau    no house我想做的是过滤注释列至少包含一个小写单词的行:ID     Name   Note1      Fin    there IS A dog outside3      Lau    no house并在列表中收集所有小写单词:my_list=['there','dog','outside','no','house']我试图过滤行是:df1=df['Note'].str.lower()为了在列表中附加单词,我想我应该首先标记字符串,然后选择所有小写的术语。我对吗?
查看完整描述

1 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

用于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']


查看完整回答
反对 回复 2023-05-16
  • 1 回答
  • 0 关注
  • 97 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信