您好我想知道如何在以下數據框中選擇包含小寫字母的行: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
提交
取消