如何使用 Python 将非结构化数据转换为结构化数据?
非结构化数据是不遵循任何特定数据模型或格式的数据,它可以采用不同的形式,例如文本、图像、音频和视频。将非结构化数据转换为结构化数据是数据分析中的一项重要任务,因为结构化数据更易于分析和提取见解。Python 提供了各种库和工具来将非结构化数据转换为结构化数据,使其更易于管理和分析。
在本文中,我们将探讨如何使用 Python 将非结构化生物特征数据转换为结构化格式,从而对数据进行更有意义的分析和解释。
虽然我们可以使用不同的方法在 Python 中将非结构化数据转换为结构化数据。在本文中,我们将讨论以下两种方法:
正则表达式 (Regex):此方法涉及使用正则表达式从非结构化文本中提取结构化数据。可以定义正则表达式模式来匹配非结构化文本中的特定模式并提取相关信息。
数据整理库:可以使用 pandas 等数据整理库来清理非结构化数据并将其转换为结构化格式。这些库提供执行数据清理、规范化和转换等操作的函数。
使用正则表达式
考虑下面显示的代码。
示例
import re import pandas as pd # 示例非结构化文本数据 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 """ # 定义正则表达式模式以提取数据 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: (.+)') # 创建空列表以存储提取的数据 ids = [] names = [] depts = [] times = [] # 遍历文本数据的每一行 for line in text_data.split('\n'): # 检查该行是否与任何正则表达式模式匹配 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)) # 使用提取的数据创建数据框 data = {'Employee ID': ids, 'Name': names, 'Department': depts, 'Punch Time': times} df = pd.DataFrame(data) # 打印数据框 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 # 加载非结构化数据 unstructured_data = pd.read_csv("unstructured_data.csv") # 从 'date_time' 列中提取日期和时间 unstructured_data['date'] = pd.to_datetime(unstructured_data['date_time']).dt.date unstructured_data['time'] = pd.to_datetime(unstructured_data['date_time']).dt.time # 将 'date_time' 列重命名为 'datetime' 并将其删除 unstructured_data = unstructured_data.rename(columns={"date_time": "datetime"}) unstructured_data = unstructured_data.drop(['datetime'], axis=1) # 透视表以获取每个员工在每个日期的"上班打卡"和"下班打卡"时间 structured_data = unstructured_data.pivot(index=['employee_id', 'date'], columns='type', values='time').reset_index() # 重命名列名 structured_data = structured_data.rename(columns={"Punch-In": "punch_in", "Punch-Out": "punch_out"}) # 通过从"下班打卡"中减去"上班打卡"来计算总工作时长 structured_data['hours_worked'] = pd.to_datetime(structured_data['punch_out']) - pd.to_datetime(structured_data['punch_in']) # 打印结构化数据 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 和正则表达式、文本解析和机器学习技术等各种方法,可以将非结构化数据转换为结构化数据。