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)
相关推荐
ponponon5 分钟前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly5 分钟前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程18 分钟前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly2 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风9 小时前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风9 小时前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风1 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风1 天前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly2 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent