gpt4 book ai didi

python - 如何在 Pandas 数据框中以小写形式过滤行和单词?

转载 作者:行者123 更新时间:2023-12-04 09:10:22 25 4
gpt4 key购买 nike

嗨,我想知道如何在以下数据框中选择包含小写的行:

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

关于python - 如何在 Pandas 数据框中以小写形式过滤行和单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63358767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com