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)
相关推荐
2301_8152795218 小时前
SQL如何利用聚合函数生成业务分析指标_KPI计算基础教程
jvm·数据库·python
qq_3300379918 小时前
mysql如何排查Out of memory错误_mysql内存分配调优
jvm·数据库·python
好家伙VCC18 小时前
**发散创新:用Rust实现基于RAFT共识算法的轻量级分布式日志系统**在分布式系统中,**一致性协议**是保障数据可靠
java·分布式·python·rust·共识算法
weixin_4585801219 小时前
如何在 Go 中直接将 AST 编译为可执行二进制文件?
jvm·数据库·python
2301_816660211 天前
PHP怎么处理Eloquent Attribute Inference属性推断_Laravel从数据自动推导类型【操作】
jvm·数据库·python
第一程序员1 天前
数据工程 pipelines 实践
python·github
知行合一。。。1 天前
Python--05--面向对象(属性,方法)
android·开发语言·python
郝学胜-神的一滴1 天前
深度学习必学:PyTorch 神经网络参数初始化全攻略(原理 + 代码 + 选择指南)
人工智能·pytorch·python·深度学习·神经网络·机器学习
qq_372154231 天前
Go 中自定义类型与基础类型的显式转换规则详解
jvm·数据库·python
LiAo_1996_Y1 天前
CSS如何实现文字渐变效果_通过background-clip实现艺术字
jvm·数据库·python