您好我想知道如何在以下数据框中选择包含小写字母的行: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']
添加回答
举报
0/150
提交
取消