偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

用于文本數(shù)據(jù)分析的 Pandas:使用 Str 訪問器清理和操作文本數(shù)據(jù)

大數(shù)據(jù) 數(shù)據(jù)分析
Pandas 中的 str 訪問器提供了許多有用的字符串操作,可以應(yīng)用于 Pandas 系列的每個元素。

文本數(shù)據(jù)是數(shù)據(jù)分析和機器學(xué)習(xí)中最常用的數(shù)據(jù)類型之一。 然而,文本數(shù)據(jù)往往是雜亂無章的,需要清洗和預(yù)處理才能被有效分析。 Pandas 是一個強大的 Python 數(shù)據(jù)分析庫,它提供了一個方便的 str 訪問器來幫助您清理和操作文本數(shù)據(jù)。

Pandas 中的 str 訪問器提供了許多有用的字符串操作,可以應(yīng)用于 Pandas 系列的每個元素。 這些操作包括字符串拆分、連接、替換等。 在這里,我們將了解一些最有用的 str 操作,它們可以幫助您清理和操作文本數(shù)據(jù)。

讓我們從使用文本數(shù)據(jù)創(chuàng)建示例數(shù)據(jù)框開始:

import pandas as pd

data = {"text_column": ["this is a text", "an example", "of text data", "in pandas"]}
df = pd.DataFrame(data)
print(df)

輸出

text_column
0 this is a text
1 an example
2 of text data
3 in pandas

1、小寫轉(zhuǎn)換

有時,文本數(shù)據(jù)可能包含混合大小寫字符,這會導(dǎo)致難以分析和比較。 str 訪問器提供了一個 lower() 方法來將所有字符轉(zhuǎn)換為小寫。

df["text_column"] = df["text_column"].str.lower()
print(df)

輸出

text_column
0 this is a text
1 an example
2 of text data
3 in pandas

2、strip方法

文本數(shù)據(jù)可能包含不需要的字符,如空格、制表符或換行符。 str 訪問器提供了 strip() 方法來從系列中每個字符串的開頭和結(jié)尾刪除這些字符。

df["text_column"] = df["text_column"].str.strip()
print(df)

輸出:

text_column
0 this is a text
1 an example
2 of text data
3 in pandas

3、替代法

str 訪問器還提供了一種 replace() 方法,用于在系列的每個元素中用一個字符串替換另一個字符串。 當(dāng)您想要替換文本數(shù)據(jù)中的特定單詞或字符時,這很有用。

df["text_column"] = df["text_column"].str.replace("text", "string")
print(df)

輸出:

text_column
0 this is a string
1 an example
2 of string data
3 in pandas

4、另一個重要的函數(shù)是extract()

此功能可用于從文本中提取特定模式。 extract() 函數(shù)將正則表達(dá)式模式作為參數(shù),并返回一個或多個匹配項作為新的 DataFrame 列。 讓我們看一個例子:

import pandas as pd

#創(chuàng)建示例 DataFrame
df = pd.DataFrame({'text': ['I love cats!', 'Dogs are the best', 'I love dogs and cats']})
# 使用 extract() 函數(shù)提取“l(fā)ove”這個詞
df['love'] = df['text'].str.extract(r'(\w+)\s(\w+)')
# Check the DataFrame
print(df)

這將產(chǎn)生以下輸出:

text   love
0 I love cats! love
1 Dogs are the best NaN
2 I love dogs and cats love

5、另一個有用的函數(shù)是split()

此函數(shù)可用于根據(jù)指定的分隔符將文本拆分為多個部分。 split() 函數(shù)返回從原始文本中拆分出來的子字符串列表。 讓我們看一個例子:

import pandas as pd

#創(chuàng)建示例 DataFrame
df = pd.DataFrame({'text': ['I love cats!', 'Dogs are the best', 'I love dogs and cats']})
# 使用 split() 函數(shù)將文本拆分為單詞
df['text'] = df['text'].str.split()
# Check the DataFrame
print(df

這將產(chǎn)生以下輸出:

text
0 [I, love, cats!]
1 [Dogs, are, the, best]
2 [I, love, dogs, and, cats]

如您所見,split() 函數(shù)已將文本拆分為單詞并返回子字符串列表。

6、去除標(biāo)點符號和特殊字符

在文本數(shù)據(jù)中,我們經(jīng)常會有很多與分析無關(guān)的標(biāo)點符號和特殊字符。 要刪除它們,我們可以使用 str.translate() 方法和 str.maketrans() 方法。

import string

#創(chuàng)建特殊字符和標(biāo)點符號到 None 的映射
translator = str.maketrans("", "", string.punctuation)
# 將翻譯器應(yīng)用于文本列
df["text"] = df["text"].str.translate(translator)

7、刪除停用詞

停用詞是對文本意義不大的常用詞,通常會被刪除以簡化分析。 要刪除停用詞,我們可以使用 nltk 庫。

import nltk
from nltk.corpus import stopwords

# 下載停用詞語料庫
nltk.download("stopwords")
# 獲取停用詞列表
stop_words = set(stopwords.words("english"))
# 從文本列中刪除停用詞
df["text"] = df["text"].apply(lambda x: " ".join([word for word in x.split() if word not in stop_words]))

8、對文本進(jìn)行詞干化或詞形還原

詞干提取和詞形還原是兩種重要的 NLP 技術(shù),用于將單詞簡化為基本形式。 它們有助于簡化文本數(shù)據(jù)并使其更易于分析。

詞干提取是將單詞簡化為基本形式或詞根形式的過程。 它涉及刪除詞的后綴或詞尾以得到詞根。 例如,“running”、“runner”和“ran”都使用詞干提取法簡化為詞根形式“run”。

另一方面,詞形還原是使用基于字典的方法將單詞簡化為基本形式的過程。 與詞干提取不同,詞形還原考慮單詞的上下文并將它們映射到最有意義的基本形式。 例如,“running”將簡化為“run”,“is”將簡化為“be”。

詞干提取和詞形還原都有各自的優(yōu)點和缺點,它們之間的選擇取決于用例。 詞干化更快更簡單,但詞形還原更準(zhǔn)確并產(chǎn)生更有意義的基本形式。

要在 Pandas 中對文本進(jìn)行詞干化或詞形還原,我們可以使用 nltk 庫。 以下是如何使用 nltk 執(zhí)行詞干提取的示例:

import nltk
from nltk.stem import PorterStemmer

# 初始化詞干分析器
stemmer = PorterStemmer()
# 定義一個函數(shù)來阻止文本
def stem_text(text):
return ' '.join([stemmer.stem(word) for word in text.split()])
# 將詞干提取功能應(yīng)用于文本列
df['text_stemmed'] = df['text'].apply(stem_text)

以下是如何使用 nltk 執(zhí)行詞形還原的示例:

import nltk
from nltk.stem import WordNetLemmatizer

#初始化詞形還原器
lemmatizer = WordNetLemmatizer()
# 定義一個函數(shù)來對文本進(jìn)行詞形還原
def lemmatize_text(text):
return ' '.join([lemmatizer.lemmatize(word) for word in text.split()])
# 將詞形還原函數(shù)應(yīng)用于文本列
df['text_lemmatized'] = df['text'].apply(lemmatize_text)

在這兩種情況下,詞干化或詞形化的文本都存儲在數(shù)據(jù)框中的新列中。 這個新列可用于進(jìn)一步分析或建模。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2020-11-06 17:42:02

Python開發(fā)工具

2023-06-11 17:00:06

2020-10-09 09:35:17

數(shù)據(jù)分析可視化

2021-03-28 08:57:57

Python 文本數(shù)據(jù)

2017-11-03 12:57:06

機器學(xué)習(xí)文本數(shù)據(jù)Python

2011-04-08 14:45:08

文本數(shù)據(jù)Oracle

2018-07-24 16:00:38

2025-07-14 07:21:00

Pandas數(shù)據(jù)分析Python

2025-07-18 07:59:56

2023-01-28 10:09:00

Pandas數(shù)據(jù)分析Python

2025-05-14 13:23:19

數(shù)據(jù)模型AI

2023-11-07 08:33:08

2025-07-09 07:50:00

2009-08-20 09:15:20

C#操作文本文件

2024-11-26 08:00:00

SQLPandasPandaSQL

2018-03-15 09:00:28

TED文本分析NLP

2024-08-23 09:00:00

2023-08-15 16:20:42

Pandas數(shù)據(jù)分析

2022-08-14 14:54:10

Pandas字符串數(shù)字類型

2024-11-04 06:20:00

Redis單線程
點贊
收藏

51CTO技術(shù)棧公眾號