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)
相关推荐
Blossom.1189 分钟前
基于深度学习的医学图像分析:使用PixelRNN实现医学图像超分辨率
c语言·人工智能·python·深度学习·yolo·目标检测·机器学习
都叫我大帅哥23 分钟前
深度学习中的"火眼金睛":卷积神经网络(CNN)终极指南
python·深度学习
都叫我大帅哥26 分钟前
幽默深度指南:LangChain中的RunnableParallel - 让AI任务像交响乐团般协同工作
python·langchain·ai编程
liliangcsdn37 分钟前
python 12 install jupyter时zmq.h或libzmq报错处理
ide·python·jupyter
_Orch1d39 分钟前
初识无监督学习-聚类算法中的K-Means算法,从原理公式到简单代码实现再到算法优化
python·学习·算法·机器学习·numpy·kmeans·聚类
青衫客361 小时前
Python 实例属性与方法命名冲突:一次隐藏的Bug引发的思考
python
人邮异步社区1 小时前
先学Python还是c++?
开发语言·c++·python
旧时光巷3 小时前
【机器学习③】 | CNN篇
人工智能·pytorch·python·机器学习·cnn·卷积神经网络·lenet-5
amazinging3 小时前
北京-4年功能测试2年空窗-报培训班学测开-第六十六天
python·学习·面试
叫我:松哥8 小时前
python案例:基于python 神经网络cnn和LDA主题分析的旅游景点满意度分析
人工智能·python·神经网络·数据挖掘·数据分析·cnn·课程设计