Найти - Пользователи
Полная версия: Найти все слова длиной от n символов в pandas dataframe с помощью regular expressions
Начало » Python для новичков » Найти все слова длиной от n символов в pandas dataframe с помощью regular expressions
1
Kurtz
Здравствуйте.
Подскажите, есть pandas dataframe с текстом, нужно найти все слова, отфильтровав все остальное, при этом оставив слова, которые, скажем, от 3-х букв и выше. Я могу это сделать с помощью re.findall, но re.findall возвращает list (для каждой строки). То есть применив
 >>> processed_text = sample_text.apply(lambda x: re.findall('[a-zA-Z]{5,}', x))  
где sample_text это pandas.core.series.Series, мы получим
 6    [Plasticity, Mediated, Competitive, Learning]
7    [Morphology, Classification, using, Analogue]
А нужно тоже самое, но в обычном для dataframe виде
 6    Plasticity Mediated Competitive Learning
7    Morphology Classification using Analogue
Kurtz
Сделал в два этапа, как сделать в один пока не понял.
 >>> text = 'Hello this is the^& test &%text with some*&* digits 12449/565)^%'
>>> preprocessed_text = re.sub('[^a-zA-Z]+', ' ', text)
>>> print(preprocessed_text)
'Hello this is the test text with some digits'
 >>> result = re.sub(r'\W*\b\w{1,4}\b', '', preprocessed_text.lower())
>>> print(result)
'hello digits'
В случае с re.sub не создаются листы и dataframe остается в первоначальном виде.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB