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. fastjson配置序列化过滤转换

    @Configuration@EnableWebMvcpublic class WebConfig implements WebMvcConfigurer { @Override public voi ...

  2. ansible-基础和安装

    什么是ansible ansible是python中的一套模块,系统中的一套自动化工具,可以用来作系统管理.自动化命令.等任务. ansible优势 (1) ansible是python中的一套完整的 ...

  3. Github个人首页美化指北

    当你尝试去创建一个与你Github用户名相同的仓库时,会发现这是Github为你预留的特殊仓库,用来作为你的Github Profile,这个仓库就相当于一个可以显示在你Github个人页的READM ...

  4. Git hub加载慢?下载慢?浏览慢?几个小技巧让你一键起飞!

    记得,那是一个风和日丽,艳阳高照的夜晚,只因为当初的一次回眸,于是便决然走向了程序员的道路,从此,CV大法心中记,代码伴我身. 这一天,正当我打开电脑准备开开心心的使用CV大法完成任务的时候,却恼人的 ...

  5. 初试Python

    01 Python简介 Python是一种跨平台的计算机程序设计语言.于1989年开发的语言,创始人范罗苏姆(Guido van Rossum),别称:龟叔(Guido). python具有非常多并且 ...

  6. 第一期chrome浏览器的小技巧------《提高搜索效率》

    !!! 这次的技巧是:利用chrome提供的设置 提高你的搜索效率 !!! 我们经常遇到问题,搜索的时候很不方便 比如你在百度上搜索一个东西的时候正好没有搜到,那么你想找到这个东西的话,很明显要到其他 ...

  7. docker19.03限制容器使用的cpu资源

    一,用--cpus限制可用的cpu个数 例子: [root@localhost liuhongdi]# docker run -idt --name kafka1 --hostname kafka1 ...

  8. Windows(WSL2) Linux子系统搭建Docker环境

    摘要:本文主要介绍了如何再Windows(WSL2)中启用Linux系统中,并搭建Docker环境. WSL是适用于 Linux 的 Windows 子系统可让开发人员按原样运行 GNU/Linux ...

  9. jquery ui,拖拽,dragsort

    一.导入js.导入jquery.dragsort.js外还需要导入jQuery. 二.HTML部分 <!DOCTYPE html> <html> <head> &l ...

  10. Anderson《空气动力学基础》5th读书笔记 第1记——流动相似性

    在飞机真正上天之前,我们常常需要制作出缩小版的模型放在风洞中吹呀吹,尽可能地模拟真实飞行中的参数,这时我们就需要实现流动相似性,这便是本记要讲的. 文章目录 一.流动相似性的标准 二.流动相似性的应用 ...