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)
相关推荐
interception11 分钟前
爬虫js逆向,jsdom补环境,抖音,a_bogus
javascript·爬虫·python
林炳然17 分钟前
Python-Basic Day-5 函数-生成器&装饰器
python
Yue丶越31 分钟前
【Python】基础语法入门(四)
linux·开发语言·python
AI街潜水的八角4 小时前
Python电脑屏幕&摄像头录制软件(提供源代码)
开发语言·python
hadage2334 小时前
--- git 的一些使用 ---
开发语言·git·python
笨笨聊运维10 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
Gerardisite10 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php