如何使用 Python 將非結構化資料轉換為結構化資料?
非結構化資料是指不遵循任何特定資料模型或格式的資料,它可以以文字、影像、音訊和影片等多種形式存在。將非結構化資料轉換為結構化資料是資料分析中一項重要的任務,因為結構化資料更容易分析和從中提取見解。Python 提供了各種庫和工具來將非結構化資料轉換為結構化資料,使其更易於管理和分析。
在本文中,我們將探討如何使用 Python 將非結構化的生物識別資料轉換為結構化格式,以便對資料進行更有意義的分析和解釋。
雖然我們可以使用不同的方法來將 Python 中的非結構化資料轉換為結構化資料,但在本文中,我們將討論以下兩種方法:
正則表示式 (Regex):此方法涉及使用正則表示式從非結構化文字中提取結構化資料。可以定義正則表示式模式以匹配非結構化文字中的特定模式並提取相關資訊。
資料整理庫:可以使用 pandas 等資料整理庫將非結構化資料清理並轉換為結構化格式。這些庫提供執行資料清理、規範化和轉換等操作的函式。
使用正則表示式
請考慮以下程式碼。
示例
import re import pandas as pd # sample unstructured text data text_data = """ Employee ID: 1234 Name: John Doe Department: Sales Punch Time: 8:30 AM Employee ID: 2345 Name: Jane Smith Department: Marketing Punch Time: 9:00 AM """ # define regular expression patterns to extract data id_pattern = re.compile(r'Employee ID: (\d+)') name_pattern = re.compile(r'Name: (.+)') dept_pattern = re.compile(r'Department: (.+)') time_pattern = re.compile(r'Punch Time: (.+)') # create empty lists to store extracted data ids = [] names = [] depts = [] times = [] # iterate through each line of the text data for line in text_data.split('\n'): # check if the line matches any of the regular expression patterns if id_pattern.match(line): ids.append(id_pattern.match(line).group(1)) elif name_pattern.match(line): names.append(name_pattern.match(line).group(1)) elif dept_pattern.match(line): depts.append(dept_pattern.match(line).group(1)) elif time_pattern.match(line): times.append(time_pattern.match(line).group(1)) # create a dataframe using the extracted data data = {'Employee ID': ids, 'Name': names, 'Department': depts, 'Punch Time': times} df = pd.DataFrame(data) # print the dataframe print(df)
解釋
首先,我們將非結構化文字資料定義為多行字串。
接下來,我們定義正則表示式模式以從文字中提取相關資料。為此,我們使用 Python 中的 re 模組。
我們建立空列表來儲存提取的資料。
我們遍歷文字資料的每一行,並檢查它是否與任何正則表示式模式匹配。如果匹配,我們將提取相關資料並將其新增到相應的列表中。
最後,我們使用提取的資料建立一個 Pandas 資料框並列印它。
輸出
Employee ID Name Department Punch Time 0 1234 John Doe Sales 8:30 AM 1 2345 Jane Smith Marketing 9:00 AM
使用 Pandas 庫
假設我們有如下所示的非結構化資料。
employee_id,date,time,type 1001,2022-01-01,09:01:22,Punch-In 1001,2022-01-01,12:35:10,Punch-Out 1002,2022-01-01,08:58:30,Punch-In 1002,2022-01-01,17:03:45,Punch-Out 1001,2022-01-02,09:12:43,Punch-In 1001,2022-01-02,12:37:22,Punch-Out 1002,2022-01-02,08:55:10,Punch-In 1002,2022-01-02,17:00:15,Punch-Out
示例
import pandas as pd # Load unstructured data unstructured_data = pd.read_csv("unstructured_data.csv") # Extract date and time from the 'date_time' column unstructured_data['date'] = pd.to_datetime(unstructured_data['date_time']).dt.date unstructured_data['time'] = pd.to_datetime(unstructured_data['date_time']).dt.time # Rename 'date_time' column to 'datetime' and drop it unstructured_data = unstructured_data.rename(columns={"date_time": "datetime"}) unstructured_data = unstructured_data.drop(['datetime'], axis=1) # Pivot the table to get 'Punch-In' and 'Punch-Out' time for each employee on each date structured_data = unstructured_data.pivot(index=['employee_id', 'date'], columns='type', values='time').reset_index() # Rename column names structured_data = structured_data.rename(columns={"Punch-In": "punch_in", "Punch-Out": "punch_out"}) # Calculate total hours worked by subtracting 'punch_in' from 'punch_out' structured_data['hours_worked'] = pd.to_datetime(structured_data['punch_out']) - pd.to_datetime(structured_data['punch_in']) # Print the structured data print(structured_data)
輸出
type employee_id date punch_in punch_out hours_worked 0 1001 2022-01-01 09:01:22 12:35:10 03:33:48 1 1001 2022-01-02 09:12:43 12:37:22 03:24:39 2 1002 2022-01-01 08:58:30 17:03:45 08:05:15 3 1002 2022-01-02 08:55:10 17:00:15 08:05:05
結論
總之,非結構化資料可能難以分析和解釋。但是,藉助 Python 和各種方法(例如正則表示式、文字解析和機器學習技術),可以將非結構化資料轉換為結構化資料。
廣告