首页 > 编程语言 >【创意、创造】用 Python pandas做一个读取Excel,再写入新的Excel小工具

【创意、创造】用 Python pandas做一个读取Excel,再写入新的Excel小工具

时间:2023-12-15 18:37:49浏览次数:29  
标签:loc sheet Python Excel back col front end pandas

Python很好很强大,1.5天时间,简化很多重复的劳动,哈哈~

 

import pandas as pd
import datetime as dt

def handleFrontEnd():
    # 处理【上周前端发版】开始
    sheet_front_end = pd.read_excel('D:\某前端原文件.xlsx',
                                    sheet_name='Sheet1', header=None)

    # columns=['时间','项目', '人员', '团队', '发布原因', '是否记为Bug']
    col_project = 0
    col_staff = 2
    sheet_front_end.dropna(inplace=True)
    print(sheet_front_end)

    for x in sheet_front_end.index:
        if (sheet_front_end.loc[x, col_project] == '项目'
                or 'xxx-' in sheet_front_end.loc[x, col_project]
                or 'xxxxx-' in sheet_front_end.loc[x, col_project]):
            sheet_front_end.drop(x, inplace=True)
            continue
        sheet_front_end.loc[x, col_staff] = sheet_front_end.loc[x, col_staff].strip()


    sheet_front_end.sort_values(by=col_staff, ascending=True, inplace=True)
  # 很方便地调整这几列前后顺序 cols = sheet_front_end.columns[[1, 0, 2]] sheet_front_end = sheet_front_end[cols] df1 = pd.DataFrame(sheet_front_end) df1.to_excel('D:\上周前端发版 new.xlsx', index=False, header=None) # 处理【上周前端发版】结束 def handleBackEnd(): # 处理【上周后端发版】开始 sheet_back_end = pd.read_excel('D:\某后端原文件.xlsx', sheet_name='Sheet1', header=None) sheet_back_end.dropna(inplace=True) for x in sheet_back_end.index: sheet_back_end.loc[x, 2] = sheet_back_end.loc[x, 2].replace('Z', '').replace('T', ' ') if ('xxx-' in sheet_back_end.loc[x, 1] or 'xxxxx-' in sheet_back_end.loc[x, 1]): sheet_back_end.drop(x, inplace=True) continue # 生产发布时间,转为北京时间,时区+8。 original_time = dt.datetime.strptime(sheet_back_end.loc[x, 2], "%Y-%m-%d %H:%M:%S") sheet_back_end.loc[x, 3] = (original_time + dt.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") # 容器 gang_num = sheet_back_end.loc[x, 1].count('-') pot_instance_name = sheet_back_end.loc[x, 1] if gang_num - 2 <= 0: sheet_back_end.loc[x, 4] = pot_instance_name else: tmp_col4_val = pot_instance_name[0:-16] if tmp_col4_val[-1] == '-': tmp_col4_val = tmp_col4_val[0:-1] sheet_back_end.loc[x, 4] = tmp_col4_val # owner,某所属人原表格文件,【被查找字段】注意依照【升序排序】。否则,VLOOKUP数据不准。 sheet_back_end.loc[x, 5] = '=VLOOKUP($E2,某所属人原表格文件!$D:$J,5)' # owner确认(是/否) sheet_back_end.loc[x, 6] = '=IF(VLOOKUP($E2,某所属人原表格文件!$D:$J,6)=0,"",VLOOKUP($E2,某所属人原表格文件!$D:$J,6))' # 使用情况 sheet_back_end.loc[x, 7] = '=IF(VLOOKUP($E2,某所属人原表格文件!$D:$J,7)=0,"",VLOOKUP($E2,某所属人原表格文件!$D:$J,7))' sheet_back_end.loc[x, 8] = '' sheet_back_end.loc[x, 9] = '' sheet_back_end.loc[len(sheet_back_end.index)] = ['原表格字段1', '原表格字段2', '原表格字段3', '生产发布时间', '容器', 'owner', 'owner确认(是/否)', '使用情况', '发布原因(含重启)', '是否记为Bug'] sheet_back_end.sort_values(by=3, ascending=False, inplace=True) print(sheet_back_end) df2 = pd.DataFrame(sheet_back_end) df2.to_excel('D:\上周后端发版 new.xlsx', index=False, header=None) # 处理【上周后端发版】结束 handleFrontEnd() handleBackEnd()

 

标签:loc,sheet,Python,Excel,back,col,front,end,pandas
From: https://www.cnblogs.com/MikeYao/p/17903579.html

相关文章

  • python flask 生产环境部署,基于gunicorn
    1.安装gunicorn,部分生产服务器会存在多个pip版本,一般用pip和pip3区分,本文中用pippipinstallgunicorn2.启动程序cd/usr/appgunicorn--workers2-b0.0.0.0:5056app:app 验证项目正常后继续如下操作3.配置gunicorn配置文件查看centos版本cat/etc/redhat-releas......
  • python3 操作csv
    https://blog.csdn.net/m0_46483236/article/details/109583685 1.python中创建新的csv文件(1).使用csv.writer()创建:代码如下:importcsvheaders=['学号','姓名','分数']rows=[('202001','张三','98'),('20200......
  • python_控制台输出带颜色的文字方法
    在python开发的过程中,经常会遇到需要打印各种信息。海量的信息堆砌在控制台中,就会导致信息都混在一起,降低了重要信息的可读性。这时候,如果能给重要的信息加上字体颜色,那么就会更加方便用户阅读了。当然了,控制台的展示效果有限,并不能像前段一样炫酷,只能做一些简单的设置......
  • python之chardet操作 编码&解码
    #python之编码&解码"""python中有两种类型,字符串和字节但是字节的编码是什么我们不知道,所以解码不好解决,chardet解决了这个问题pipinstallchardet"""#字节--->字符串importchardettemp_bytes=b'helloword'temp_str=temp_bytes.decode("utf8")print(f&quo......
  • Python 异步编程之yield关键字
    背景介绍在前面的篇章中介绍了同步和异步在IO上的对比,从本篇开始探究python中异步的实现方法和原理。python协程的发展流程:python2.5为生成器引用.send()、.throw()、.close()方法python3.3为引入yieldfrom,可以接收返回值,可以使用yieldfrom定义协程Python3.4加入了asy......
  • 数据表转换为分类Excel(if语句)
    switch/case组件:使用kettle的组件swich/case对原有表进行条件判断,分类:需求:表输入switch/case分类出2个Excel表格:第一步:构建数据流图:如果方向不对,可以进行反转方向:一:表输入:选定对应的表内容:选择t_phoneinfo表二:编辑条件控制switch/case按住shift键,将组件按照下图方式连......
  • 12.15---python文件读取
    withopen('pi_digits.txt')asfile:contents=file.read()print(contents.strip())要想访问文件内容需要先打开它才能访问,函数open()接受一个参数:要打开文件的名称。在当前执行文件的目录中查找文件名。代码中,open('E:/python/文件和异常/pi_digits.txt')返回一个表示......
  • win32com:excel筛选 复制
    importos.pathfromwin32com.clientimportDispatchexcel=Dispatch("ket.Application")#启动wpsexcel应用。Microsoftexcel用Excel.Applicationexcel.Visible=Trueend_file_basepath=r'C:\Users\meiya\Desktop\数据分省\地区'source_basepat......
  • python二分类模型精度低怎么办
    在二分类模型中,如果模型的精度较低,可能需要采取一些措施来改进模型性能。本文将介绍一些常见的方法和技巧,帮助提高二分类模型的精度。1.数据预处理确保对数据进行适当的预处理是提高模型精度的重要步骤。常见的数据预处理方法包括:-数据清洗:处理缺失值、异常值等。-特征选择:选择对目......
  • 如何在 python 中安装 torch
    PyTorch是一款功能强大的深度学习框架,它提供了丰富的工具和接口来支持各种深度学习任务。本文将介绍在Python中安装PyTorch的步骤和方法,以帮助读者快速开始使用PyTorch。1.安装Python首先,确保你的计算机上已经安装了Python。建议使用Python的最新版本,可以从官方下载并安装Python。2......