python数据写入excel文件

主要思路:数据 转DataFrame后写入excel文件

一、数据格式为字典形式1

k = e , v = '1', 'e', 0.83, 437, 0.6, 0.8, 0.9, '好'

1、这种方法使用了 from_dict 方法,指定了 orient='index' 表示使用字典的键作为行索引,然后指定了列标签为 'col1', 'col2', 'col3', 'col4'

df_out_dict_pv = pd.DataFrame.from_dict(out_dict_pv, orient='index', columns='app_id', 'app_name', 'score', 'pv' + 'ctr', 'simi', 'dl_ratio')
2、这种方法使用了 DataFrame 的构造函数,首先提取了字典的值作为数据,使用字典的键作为行索引,然后指定了列标签为 'col1', 'col2', 'col3', 'col4'

df_out_dict_pv = pd.DataFrame(out_dict_pv.values(), index=out_dict_pv.keys(), columns='col1', 'col2', 'col3', 'col4')

示例代码:

python 复制代码
import pandas as pd
for k in diff_no_key:
    print(k, diff_no_key[k])
    break
# e ['1', 'e', 0.83, 437, 0.6, 0.8, 0.9, '好']

col_name = ['app_id', 'app_name', 'score', 'pv'] + ['ctr', 'simi', 'dl_ratio'] + ["type"]
df_diff_no_key = pd.DataFrame.from_dict(diff_no_key, orient='index', columns=col_name)
df_diff_no_key.head()
df_diff_no_key.to_excel("./sim_act.xlsx", sheet_name='相关性')
print("**************end")

二、数据格式为字典形式2

python 复制代码
# new_res = []
out_data = {}
out_data['搜索词'] = query
out_data['pv'] = level
out_data['new_res'] = new_res
out_data['new_res_id'] = new_res_id
out_data['old_res'] = old_res
out_data['old_res_id'] = old_res_id


# out_file = 'out-newv4-' + file_name + '_pred.xlsx'
# out_file = out_file.replace(".xlsx_pred", "_pred")
out_file = '20241029-out-4500q-' + '10.23点检query.xlsx'
# df = pd.DataFrame(out_data)
# 将数据框写入Excel文件
# input_infos.to_excel(file_path + out_file, index=False, sheet_name='原神')
df = pd.DataFrame(out_data)
df.to_excel(out_file, index=False)
print("done  ", out_file)
相关推荐
我是坏垠10 小时前
Crypto、Cipher与Password:Java加密开发的三个核心概念
java·开发语言·python
测试秃头怪11 小时前
如何评估自动化测试脚本的编写时间和维护工作量?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
姚青&11 小时前
测试左移体系
python·自动化
技术民工之路11 小时前
Python Socket编程零基础实战教程(TCP/UDP通信完整版)
python·tcp/ip·udp
EW Frontier12 小时前
三级跳突破864维动作空间——QMIX-Hierarchical多无人机协同通信方法全解析【附python代码】
开发语言·python·无人机·强化学习·通信资源分配
中微极客13 小时前
TensorFlow模型量化实战:从精度到延迟的优化指南
人工智能·python·tensorflow
仙人球部落14 小时前
-python-LangGraph框架(3-31-LangGraph 「合并式状态管理」的原理与实践)
开发语言·javascript·python
蜡笔削薪15 小时前
财联支付异地拓展商户的区域限制是否符合监管规定?
大数据·python
印度神油915 小时前
Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
windows·python·ci/cd