pandas的文本与序列化

文章目录

1.pandas的文本与序列化

python 复制代码
result_data = pd.DataFrame(json_data_list)

with open(jsonl_file_path, 'w', encoding='utf-8') as jsonl_file:
    result_data.to_json(orient='records', lines=True, force_ascii=False, path_or_buf=jsonl_file)
python 复制代码
数据不换行
df.at[i, column_name_transcript] = df.at[i, column_name_transcript].split('\n')

pandas转序列化数据

python 复制代码
data_dicts = df.to_dict(orient='records')

with open(jsonl_file_path, 'w', encoding='utf-8') as jsonl_file:
    for data in data_dicts:
        # 将字典转换为JSON字符串,ensure_ascii=False参数确保中文字符不会被转义
        # 写入文件时,每个JSON对象后面跟着一个换行符
        jsonl_file.write(json.dumps(data, ensure_ascii=False) + '\n')

pandas元素序列化

python 复制代码
    df['column_01'] = df['column_01'].apply(
        lambda x: json.dumps(x, ensure_ascii=False) if isinstance(x, str) else ''
    )
python 复制代码
 # 对"answer"列中的每个字符串元素去除空白并分割成单词列表
    df['question'] = df['question'].apply(lambda x: x.strip().split())
    df['answer'] = df['answer'].apply(lambda x: x.strip().split())

    # 序列化"answer"列中的每个元素为JSON格式的字符串
    df['question'] = df['question'].apply(lambda x: json.dumps(x, ensure_ascii=False) if isinstance(x, list) else x)
    df['answer'] = df['answer'].apply(lambda x: json.dumps(x, ensure_ascii=False) if isinstance(x, list) else x)
相关推荐
一只小波波呀1 小时前
打卡第48天
python
zstar-_1 小时前
一套个人知识储备库构建方案
python
Amo Xiang1 小时前
《100天精通Python——基础篇 2025 第5天:巩固核心知识,选择题实战演练基础语法》
python·选择题·基础语法
江梦寻2 小时前
MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)
开发语言·后端·python·macos·架构·策略模式
霖檬ing2 小时前
Python——MySQL远程控制
开发语言·python·mysql
miniwa2 小时前
Python编程精进:CSV 模块
python
老胖闲聊9 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1189 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之9 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
lyaihao10 小时前
使用python实现奔跑的线条效果
python·绘图