Python与VBA的比较2

需求:

input文件中有两列数据,第一列为Name,第二列为Score,Name列里有重复的值,要求按照name的唯一值统计 score,输出到output文件按中。

1--用 Pandas解决:

from pandas import DataFrame
import pandas as pd df=pd.read_excel(r"C:\Users\12078\Desktop\UIPATH_test\20200409\input.xlsx",sheet_name='Sheet1')
df=df.groupby('Name').sum()
df.to_excel(r"C:\Users\12078\Desktop\UIPATH_test\20200409\output.xlsx",sheet_name="Sheet1")

2--用 VBA解决:

Option Explicit
Option Base 1 Sub test_data()
on error goto errorhandling
Dim wb_in As Workbook
Dim wb_out As Workbook
Dim sht_in As Worksheet
Dim sht_out As Worksheet
Dim rng As Range
Dim usedrows As Integer
Dim usedrows_out As Integer
Dim input_path As String
Dim output_path As String Dim data_dict As Object
Dim data_arr As Variant
Dim data_arr_out As Variant input_path = "C:\Users\12078\Desktop\UIPATH_test\20200409\input.xlsx"
output_path = "C:\Users\12078\Desktop\UIPATH_test\20200409\output.xlsx" Set wb_in = checkAndAttachWorkbook(input_path)
Set sht_in = wb_in.Worksheets("Sheet1")
Set wb_out = Workbooks.Add
wb_out.SaveAs output_path
Set sht_out = wb_out.Worksheets("Sheet1") Set data_dict = CreateObject("Scripting.Dictionary")
usedrows = WorksheetFunction.Max(getLastValidRow(sht_in, "A"), getLastValidRow(sht_in, "B"))
data_arr = sht_in.Range("A2", "B" & usedrows) Dim i As Integer
For i = 1 To UBound(data_arr, 1)
If Not data_dict.Exists(data_arr(i, 1)) Then
data_dict.Add data_arr(i, 1), data_arr(i, 2)
Else
data_dict(data_arr(i, 1)) = data_dict(data_arr(i, 1)) + data_arr(i, 2)
End If
Debug.Print data_arr(i, 1) & "--" & data_dict(data_arr(i, 1))
Next i sht_out.Range("A1") = "Name"
sht_out.Range("B1") = "Score"
usedrows_out = data_dict.Count Dim index_dict As Integer
ReDim data_arr_out(1 To UBound(data_dict.keys) + 1, 1 To 2)
For index_dict = 0 To UBound(data_dict.keys)
data_arr_out(index_dict + 1, 1) = data_dict.keys()(index_dict)
data_arr_out(index_dict + 1, 2) = data_dict(data_dict.keys()(index_dict))
Debug.Print index_dict
Debug.Print data_arr_out(index_dict + 1, 1) & "--" & data_arr_out(index_dict + 1, 2) 'for debug
Next
sht_out.Range("A2").Resize(UBound(data_arr_out), 2) = data_arr_out Call checkAndCloseWorkbook(wb_in, False)
Call checkAndCloseWorkbook(wb_out, True)
Exit Sub
errorhandling:
Call checkAndCloseWorkbook(wb_in, False)
Call checkAndCloseWorkbook(wb_out, False)
End Sub ' 辅助函数:
'Get last row of Column N in a Worksheet
Function getLastValidRow(in_ws As Worksheet, in_col As String)
getLastValidRow = in_ws.Cells(in_ws.Rows.Count, in_col).End(xlUp).Row
End Function Function checkAndAttachWorkbook(in_wb_path As String) As Workbook
Dim wb As Workbook
Dim mywb As String
mywb = in_wb_path For Each wb In Workbooks
If LCase(wb.FullName) = LCase(mywb) Then
Set checkAndAttachWorkbook = wb
Exit Function
End If
Next Set wb = Workbooks.Open(in_wb_path, UpdateLinks:=0)
Set checkAndAttachWorkbook = wb End Function Function checkAndCloseWorkbook(in_wb_path As String, in_saved As Boolean)
Dim wb As Workbook
Dim mywb As String
mywb = in_wb_path
For Each wb In Workbooks
If LCase(wb.FullName) = LCase(mywb) Then
wb.Close Savechanges:=in_saved
Exit Function
End If
Next
End Function

输出结果:

比对结论:

pandas简单得多!

Pandas_VBA_数据分类比较的更多相关文章

  1. Pandas_VBA_数据筛选比较

    Pandas与VBA筛选数据的比较 Author:Collin_PXY 需求: 将B列里值为Completed 和 Pending的A,B,D三列数据筛选出来,新建一个名为 Filited_data的 ...

  2. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  3. App开发:模拟服务器数据接口 - MockApi

    为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...

  4. 使用TSQL查询和更新 JSON 数据

    JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...

  5. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  6. SQLSERVER将一个文件组的数据移动到另一个文件组

    SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...

  7. 【.net 深呼吸】设置序列化中的最大数据量

    欢迎收看本期的<老周吹牛>节目,由于剧组严重缺钱,故本节目无视频无声音.好,先看下面一个类声明. [DataContract] public class DemoObject { [Dat ...

  8. Scrapy框架爬虫初探——中关村在线手机参数数据爬取

    关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...

  9. 通过AngularJS实现前端与后台的数据对接(二)——服务(service,$http)篇

    什么是服务? 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性. 服务是一个单例对象,在每个应用中只会被实例化一次(被$injector实例化) ...

随机推荐

  1. RHSA-2017:2029-中危: openssh 安全和BUG修复更新(存在EXP、代码执行、本地提权)

    [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...

  2. ps 安装 ps 2017 下载 及教程(保姆式教程)

    链接:https://pan.baidu.com/s/1GJHiwmxwRApFYhyNZBCQtQ 提取码:7r6u 以上是百度网盘的地址. 1.下载解压安装前先断网在安装点击set-up 软件,之 ...

  3. 多测师讲解selenium_运行报告相出错归纳_高级讲师肖sir

    <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> EETraceback (most recent c ...

  4. Verilog基础入门——简单的语句块编写(二)

    [题干] [代码] module top_module( input a, input b, output out ); assign out = a&b; endmodule [与或非门等] ...

  5. empty()和size() == 0有区别吗

    empty()和size() 这里说的empty()和size()都是STL的容器中提供的接口,分别用来判断当前容器是否为空和获取当前包含的元素个数 区别 其实按道理来说两者应该是相等的,而且STL容 ...

  6. 好用的C语言编程软件!工具都没有,怎么用技术改变世界呢!

    好用的C语言编程软件 1.VS(Visual Studio)   VS(Visual Studio) VS是目前最流行的windows平台应用程序的集成开发环境,由于大部分同学使用的都是Windows ...

  7. es7.8启动报错 说是主节点没找到

    p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1 ...

  8. centos8安装java jdk 13

    一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...

  9. linux磁盘空间满了

    df-h 看到根目录已经用满了! find / -size +100M |xargs ls -lh列出系统内大于100M的文件 du --max-depth=1 -h 查看当前目录内文件夹的大小 看一 ...

  10. Bitmap 创建、转换、圆角、设置透明度

    指定一个色值生成bitmap  public Bitmap getBackGroundBitmap(int color) { Paint p = new Paint(); p.setColor(Col ...