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 小时前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li3 小时前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸8 小时前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学8 小时前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田1 天前
Pydantic校验配置文件
python
hboot1 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi2 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽2 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187912 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python