簡體   English   中英

如何在數據框列中找到特定的表達式?

[英]How Do I Find a Specific Expression within a Dataframe Column?

我有一個數據框,其中有一列稱為“描述”。 我想瀏覽此列中的所有文本,並標識那些描述包含至少3位數字的行。

我在這里:

import re 
df['StrDesc'] = df['Description'].str.split()
y=re.findall('[0-9]{3}',str(df['StrDesc'])
print(y)

我將文本列轉換為字符串。 在使用最終的正則表達式之前,是否需要運行for循環來遍歷每一行?

我是最好的方式嗎?

我的錯誤是“解析時出現意外的EOF”。

使用str.findall ,無需split

y = df['Description'].str.findall('[0-9]{3}')

但是通過一些測試, 一般的解決方案有點復雜:

df = pd.DataFrame({'Description':['354 64 133 5867 4 te345',
                                  'rt34 3tyr 456',
                                  '23 gh346h rt 9404']})

print(df)
               Description
0  354 64 133 5867 4 te345
1            rt34 3tyr 456
2        23 gh346h rt 9404

y = df['Description'].str.findall('(?:(?<!\d)\d{3}(?!\d))')
print (y)
0    [354, 133, 345]
1              [456]
2              [346]
Name: Description, dtype: object

暫無
暫無

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

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