如何用Pandas将目录数据提取为规范表格形式
解决Pandas处理多值字段生成表格的问题
针对你提到的目录数据处理需求,核心是处理LLpHomeDirectory的多值展开,以下分两种常见数据场景给出具体实现方案:
场景1:原始数据为字典列表(LLpHomeDirectory为列表类型)
如果你的原始数据是Python字典列表,其中LLpHomeDirectory本身就是列表格式,直接用Pandas的explode()方法即可展开多值:
示例代码
import pandas as pd # 模拟你的原始目录数据 raw_data = [ { "costCenter": "CC001", "mail": "user1@example.com", "LLpResponsible": "John Doe", "LLpHomeDirectory": ["dir1/path1", "dir1/path2"], "fullName": "User One" }, { "costCenter": "CC002", "mail": "user2@example.com", "LLpResponsible": "Jane Smith", "LLpHomeDirectory": ["dir2/path1"], "fullName": "User Two" } ] # 转换为DataFrame df = pd.DataFrame(raw_data) # 展开多值字段,同时重置索引 result_df = df.explode("LLpHomeDirectory", ignore_index=True) # 提取指定字段(若原始数据有多余字段,通过此步骤筛选) result_df = result_df[["costCenter", "mail", "LLpResponsible", "LLpHomeDirectory", "fullName"]] # 输出结果 print(result_df)
输出结果
| costCenter | LLpResponsible | LLpHomeDirectory | fullName | |
|---|---|---|---|---|
| CC001 | user1@example.com | John Doe | dir1/path1 | User One |
| CC001 | user1@example.com | John Doe | dir1/path2 | User One |
| CC002 | user2@example.com | Jane Smith | dir2/path1 | User Two |
场景2:原始数据为文本格式(LLpHomeDirectory用分隔符拼接)
如果你的原始数据是CSV/TSV等文本格式,LLpHomeDirectory的多值用分隔符(如分号、逗号)拼接成字符串,需先分割为列表再展开:
示例代码(以CSV为例,分隔符为分号)
import pandas as pd # 读取CSV文件(替换为你的实际文件路径) df = pd.read_csv("directory_data.csv") # 将拼接的字符串分割为列表(替换为你的实际分隔符) df["LLpHomeDirectory"] = df["LLpHomeDirectory"].str.split(";") # 展开多值字段并重置索引 result_df = df.explode("LLpHomeDirectory", ignore_index=True) # 筛选指定字段 result_df = result_df[["costCenter", "mail", "LLpResponsible", "LLpHomeDirectory", "fullName"]] # 输出结果 print(result_df)
额外处理说明
- 若
LLpHomeDirectory存在空值,可添加dropna(subset=["LLpHomeDirectory"])过滤空行:result_df = df.explode("LLpHomeDirectory", ignore_index=True).dropna(subset=["LLpHomeDirectory"]) - 若多值的分隔符不是分号,替换
str.split()中的参数即可(如str.split(","))
内容的提问来源于stack exchange,提问作者user2023
相关产品推荐
相关产品推荐

