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)
相关推荐
Learn Beyond Limits9 小时前
Correlation vs Cosine vs Euclidean Distance|相关性vs余弦相似度vs欧氏距离
人工智能·python·神经网络·机器学习·ai·数据挖掘
专注于大数据技术栈9 小时前
java学习--==和equals
java·python·学习
testtraveler10 小时前
[Fix] ImportError: libtorch_cpu.so: undefined symbol: iJIT_NotifyEvent
pytorch·python·bug
lang2015092810 小时前
Kafka延迟操作机制深度解析
分布式·python·kafka
测试老哥11 小时前
软件测试:测试用例的设计
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
koo36412 小时前
pytorch环境配置
人工智能·pytorch·python
程序员杰哥15 小时前
Python自动化测试之线上流量回放:录制、打标、压测与平台选择
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试
吴佳浩15 小时前
LangChain v1 重大更新讲解⚠⚠⚠
python·langchain·agent
顾安r17 小时前
11.20 开源APP
服务器·前端·javascript·python·css3
萧鼎18 小时前
Python PyTesseract OCR :从基础到项目实战
开发语言·python·ocr