簡體   English   中英

如何在 pandas dataframe 中過濾小寫的行和單詞?

[英]How to filter rows and words in lower case in pandas dataframe?

嗨,我想知道如何在以下 dataframe 中包含小寫字母的 select 行:

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()

對於在列表中附加單詞,我認為我應該首先標記字符串,然后 select 所有小寫術語。 我對嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM