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)
相关推荐
JavaEdge在掘金3 分钟前
启动nginx报错,80 failed (97: Address family not supported by protocol)
python
纪元A梦10 分钟前
华为OD机试真题——绘图机器(2025A卷:100分)Java/python/JavaScript/C++/C/GO最佳实现
java·javascript·c++·python·华为od·go·华为od机试题
程序员小远22 分钟前
接口测试和单元测试详解
自动化测试·软件测试·python·测试工具·单元测试·测试用例·接口测试
Tech Synapse31 分钟前
电商商品推荐系统实战:基于TensorFlow Recommenders构建智能推荐引擎
人工智能·python·tensorflow
聿小翼35 分钟前
selenium-wire 与 googletrans 的爱恨情仇
python
咖啡调调。37 分钟前
模板引擎语法-算术运算
python·django·sqlite
CodeCraft Studio38 分钟前
Excel处理控件Spire.XLS系列教程:Java设置Excel活动工作表或活动单元格
java·python·excel
Doker 多克38 分钟前
Python-Django系列—部件
开发语言·python
Linux运维老纪40 分钟前
Python文件操作及数据库交互(Python File Manipulation and Database Interaction)
linux·服务器·数据库·python·云计算·运维开发
Bruce_Liuxiaowei1 小时前
MCP Python SDK构建的**SQLite浏览器**的完整操作指南
数据库·python·sqlite