首页 技术 正文
技术 2022年11月14日
0 收藏 977 点赞 4,908 浏览 2789 个字

Python与VBA的比较2

需求:

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

1–用 Pandas解决:

from pandas import DataFrame
import pandas as pddf=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 1Sub 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 StringDim 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 FunctionFunction 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 = wbEnd FunctionFunction 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简单得多!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902