dataframe在最后新增一行_【TOOLS】python中对Excel进行写入操作,写入一列或者一行(openpyxl)...
前言
最近在做expansion of datset,所以需要把扩展的dataset写入到excel中 我已经矩阵运算全部搞定,最终输出的是两个输出 labels 和 features 自己整理为以下格式
label = [[0],[1],[2],[3]]
feature = [[0.1, 0.2, 0.3, 0.4, 0.5],[0.11, 0.21, 0.31, 0.41, 0.51],[0.6, 0.7, 0.8, 0.9, 1.00],[1.1, 1.2, 1.3, 1.4, 1.5],]
解决方案
先是准备用python带的xlrd xlrd 等包来操作感觉真的不太行 换思路,用第三方包openpyxl来操作
pip install openpyxl
官方文档在这里 https://openpyxl.readthedocs.io/en/stable/index.html
代码
废话不多说,show you my code
# coding=utf-8
from openpyxl import Workbook
import numpy as npwb = Workbook()ws = wb.create_sheet("che")
label = [[0],[1],[2],[3]]
feature = [[0.1, 0.2, 0.3, 0.4, 0.5],[0.11, 0.21, 0.31, 0.41, 0.51],[0.6, 0.7, 0.8, 0.9, 1.00],[1.1, 1.2, 1.3, 1.4, 1.5],]
#这个地方之所以 变成numpy格式是因为在很多时候我们都是在numpy格式下计算的,模拟一下预处理
label = np.array(label)
feature = np.array(feature)label_input = []
for l in range(len(label)):label_input.append(label[l][0])ws.append(label_input)
for f in range(len(feature[0])):ws.append(feature[:, f].tolist())wb.save("chehongshu.xlsx")
结果生成一个excel,最后结果如下图:
总结
openpyxl包用起来是真的方便,对于写入,只需要建立一个LIST进行append就好了,如果excel为空的那append就从第一行开始递增操作,你也可以理解为一个ws.append()操作就相当于写入一行,如果excel为有数据的时候,那写入操作从没有数据的那一行开始写入;这里也说一下本来想用Insert来着但是忽略了一个条件,就是insert有个前提条件就是For example to insert a row at 7 (before the existing row 7):,意思为插入之前你的数据的大小一定是比要插入的行数或者列数大的,也就是说插入只能插到里面,不能在边缘插。
插入核心参考代码
for col in range(len(label)):print colws.insert_cols(col+1)for index, row in enumerate(ws.rows):#print rowif index == 0:#row[col+1].value = label[col][0]print "label"print label[col]else:print "feature"print feature[col][index-1]#row[col+1].value = feature[col][index-1]
读取代码
def create_data_expansion(path, sheet):data_init = pd.read_excel(path, sheet)# print data_initdata_df = pd.DataFrame(data_init)print data_dfdata_df_transponse = data_df.Tlabel_expansion = np.array(data_df_transponse.index)label_expansion_l = []for l in range(len(label_expansion)):label_expansion_l.append([l])feature_expansion = np.array(data_df_transponse)label_expansion = np.array(label_expansion_l)return label_expansion, feature_expansionif __name__ == "__main__":path_name = "excel_demo.xlsx"sheet_name = "11"label, feature = create_data_expansion(path_name, sheet_name)print labelprint feature
结果:
dataframe在最后新增一行_【TOOLS】python中对Excel进行写入操作,写入一列或者一行(openpyxl)...相关推荐
- python显示表格_在Python中获取Excel表格的数量
How get number of sheet in below python example? file = self.excel_file.decode('base64') excel_fileo ...
- python写打开的excel 冲突_在Python中打开excel文件时出错
嗨,我对python很陌生,这里我试图用python代码打开xls文件,但它显示了一些错误,如下所示.在 代码:from xlrd import open_workbook import os.pat ...
- python 文件时间戳_在Python中每小时将时间戳记写入文件
我有一个python脚本,该脚本不断从Twitter抓取数据并将消息写入文件.我的问题是每小时,我希望程序将当前时间写入文件.下面是我的脚本.当前,它进入了时间戳功能,并一直保持每10秒打印一次时间. ...
- python怎么保存excel文件夹_在Python中保存excel工作簿
我是Python新手,我需要打开文件夹中的所有excel(一个接一个),等待SAS addin加载新数据,然后保存并关闭它.我试着使用一些库,但无法获得预期的结果.在 使用子流程:import os ...
- python在excel中数据画线_在python中使用excel工作表中的数据绘制图形
我做了一些假设.假设你的数据是这样的:x y yerr_positive yerr_negative 1 1 0.1 0.2 2 2 0.1 0.2 3 3 0.1 0.2 4 4 0.1 0.2 我 ...
- [转载] Python中对 Excel 的相关详细操作(读取、写入、指定行、指定列、新增sheet工作表)
参考链接: 使用Python写入Excel工作表 工作中用到这点,就简单的整理了一下 目录 一.准备cateContent.xlsx工作簿 二.Python中关于Excel表格常用操作总结: 2.1 ...
- python 分割字串_在Python中分割,连结和连结字串
python 分割字串 There are few guarantees in life: death, taxes, and programmers needing to deal with str ...
- python操作目录_详解python中的文件与目录操作
详解python中的文件与目录操作 一 获得当前路径 1.代码1 >>>import os >>>print('Current directory is ',os. ...
- python中返回上一步操作_通过实例解析Python文件操作实现步骤
当程序运行时,变量是保存数据的好方法,但变量.序列以及对象中存储的数据是暂时的,程序结束后就会丢失,如果希望程序结束后数据仍然保持,就需要将数据保存到文件中. Python 提供了内置的文件对象,以及 ...
- python文件读取方法read(size)的含义是_在Python中可使用read([size])来读取文件中的数据,如果参数size省略,则读取文件中的()。(4.0分)_学小易找答案...
[单选题]文本文件存储的是(),由若干文本行组成,通常每行以换行符 '\n' 结尾.(4.0分) [单选题]()属性是返回被打开文件的访问模式.(4.0分) [单选题]重力坝是由砼或( )修筑而成的大 ...
最新文章
- 如何用 Python 进行基于深度学习的计算机视觉项目开发?
- 802.11协议精读9:初探节能模式(PS mode)与缓存机制
- 018 jquery中的事件
- 定时器控件timer winform 114869229
- java数字转换为日期_Java 日期字符串date与数字long之间的转换
- 程序员面试金典——4.7最近公共祖先
- 模式识别算法中英文对照
- html5 flash播发器,什么时候HTML5视频应该回归到Flash播放器?
- c语言:用牛顿迭代法求方程在1.5附近的根:2x^3-4x^2+3x-6=0.
- java轿煤悝炾厍桴,最让人放心的汉字笔画序库.doc
- 用maven的 Mybatis代码生成器
- 优化 ASP 应用程序和 VBScript 的技巧。
- 在Win32程序中创建OpenGL渲染环境
- c语言 电话簿 链表,C语言_链表_结构体_电话簿简单实现
- CSS性能优化的几个技巧
- 十年•杭研大咖说 | 邱似峰:从应届生到网易视频云CTO的蜕变
- 基于51单片机的酒精浓度检测仪设计
- 几个不错的开源BPM系统
- 梦想CAD控件com接口界面控制右键弹出菜单
- 我与MVVM的恩怨情仇