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. CentOS7下RabbitMQ服务安装配置 (亲测有效)

    erlang 21.3 rabbitmq-server 3.7.14 下载地址 链接: https://pan.baidu.com/s/1g_T1Q_6zpyO3AepS0ZPgYQ 提取码: abq ...

  2. History和Screen的对象属性

    History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问. 属性 说明 length 返回历史列表中的网址数 History 对象方法 方法 说明 b ...

  3. 1.1 Python 概述

    1.1 Python 概述 1.1.1 了解Python Python 是由荷兰人 Guido Van Rossum 发明的一种面向对象的解释型高级编程语言.Python的设计哲学为 优雅.明确和简单 ...

  4. 一口气看完45个寄存器,CPU核心技术大揭秘

    序言 前段时间,我连续写了十来篇CPU底层系列技术故事文章,有不少读者私信我让我写一下CPU的寄存器. 寄存器这个太多太复杂,不适合写故事,拖了很久,总算是写完了,这篇文章就来详细聊聊x86/x64架 ...

  5. 【转】time 模块详解(时间获取和转换)

    转自鱼C论坛--https://fishc.com.cn/forum.php?mod=viewthread&tid=51326&highlight=time time 模块 -- 时间 ...

  6. Java异常ClassCastException

    在说ClassCastException之前,先介绍下引用类型转换: 引用类型转换分为向上转型和向下转型两种: 向上转型:多态本身是子类类型向父类类型向上转换的过程,这个过程是默认的:当父类引用指向一 ...

  7. 【DeepLearning】LeNet-5

    手写字体识别模型LeNet5诞生于1994年,是最早的卷积神经网络之一.原文地址为Gradient-Based Learning Applied to Document Recognition,感谢网 ...

  8. SharedPreferences源码分析

    分析达成目标 了解基本实现 SharePreferences是否线程安全 SharePreferences的mode参数是什么 了解apply与commit的区别 导致ANR的原因 Android8. ...

  9. Linux小项目/rhel-基于同步官网yum仓库数据搭建本地yum服务器

    本文的实验环境:aws上的Redhat 7.x , 同样也适用于Centos 7.x 简单说主要分为三步: (1) 向官网同步yum数据,可以根据具体情况,创建脚本及配置周期例行任务  (2) 搭建w ...

  10. .NET CORE 下如何使用国产数据库进行 开发

    主流国产数据库 随着贸易战的升级 ,自主研发和知识产权也是一个大的趋势,达梦和人大金仓是国产数据库中比较主流的 1.达梦数据库 更接近Oracle,更偏向自主研发,对开发人员友好度不如金仓 达梦公司在 ...