使用.apply()来比较元素(use of .apply() for comparing elements)
我有一个有数千个项目的数据框 ,其中列“ 组 ”的值重复两到十次。 该数据框有七列,其中之一被命名为“ url ”; 另一个“ 旗帜 ”。 所有这些都是字符串。
我想用熊猫来遍历这些群体。 对于每个组,我希望在“ url ”列中找到最长的项目,并在与该项目对应的“ 标志 ”列中存储“0”或“1”。 我尝试了以下方法,但无法使其工作。 我想1)摆脱下面的循环,2)能够通过df.apply(...)比较组中的所有项目
all_groups = df["group"].drop_duplicates.tolist() for item in all_groups: df[df["group"]==item].apply(lambda x: Here I would like to compare the items within one group)
可以应用()和lambda在这种情况下使用? 任何更快的方式来实现这一点?
谢谢!
I have a dataframe df of thousands of items where the value of the column "group" repeats from two to ten times. The dataframe has seven columns, one of them is named "url"; another one "flag". All of them are strings.
I would like to use Pandas in order to traverse through these groups. For each group I would like to find the longest item in the "url" column and store a "0" or "1" in the "flag" column that corresponds to that item. I have tried the following but I can not make it work. I would like to 1) get rid of the loop below, and 2) be able to compare all items in the group through df.apply(...)
all_groups = df["group"].drop_duplicates.tolist() for item in all_groups: df[df["group"]==item].apply(lambda x: Here I would like to compare the items within one group)
Can apply() and lambda be used in this context? Any faster way to implement this?
Thank you!
原文:https://stackoverflow.com/questions/45578600
最满意答案
试试这个:
Sub matrix() Dim arr() As Variant Dim totrow As Long Dim j As Long Dim t As Long Dim p As Long Dim x As Long Dim ws As Worksheet Set ws = Sheets("Sheet1") 'Set your array of numbers arr = Array(2, 3, 4) 'If you want to refer to ranges on Sheet1 use: 'arr = Array(ws.Range("T1"), ws.Range("U1"), ws.Range("V1")) totrow = 1 For j = LBound(arr) To UBound(arr) totrow = totrow * arr(j) x = x + arr(j) Next j ws.Range(ws.Cells(1, 1), ws.Cells(totrow, x)).Value = 0 p = 1 For j = UBound(arr) To LBound(arr) Step -1 For t = 1 To totrow Step 1 For i = 1 To arr(j) ws.Range(ws.Cells(t, x - arr(j) + i), ws.Cells(t + p - 1, x - arr(j) + i)).Value = 1 t = t + p Next i t = t - 1 Next t p = p * arr(j) x = x - arr(j) Next j End Sub
这适用于数组中的任何值或数组中的任意数量的整数。 主要限制是工作表上的行数和列数。
Try this:
Sub matrix() Dim arr() As Variant Dim totrow As Long Dim j As Long Dim t As Long Dim p As Long Dim x As Long Dim ws As Worksheet Set ws = Sheets("Sheet1") 'Set your array of numbers arr = Array(2, 3, 4) 'If you want to refer to ranges on Sheet1 use: 'arr = Array(ws.Range("T1"), ws.Range("U1"), ws.Range("V1")) totrow = 1 For j = LBound(arr) To UBound(arr) totrow = totrow * arr(j) x = x + arr(j) Next j ws.Range(ws.Cells(1, 1), ws.Cells(totrow, x)).Value = 0 p = 1 For j = UBound(arr) To LBound(arr) Step -1 For t = 1 To totrow Step 1 For i = 1 To arr(j) ws.Range(ws.Cells(t, x - arr(j) + i), ws.Cells(t + p - 1, x - arr(j) + i)).Value = 1 t = t + p Next i t = t - 1 Next t p = p * arr(j) x = x - arr(j) Next j End Sub
This will work for any value in the array or any number of integers in the array. The main limitation is the number of rows and columns on the sheet.
相关问答
更多-
动态为VBA中的循环(Dynamic For Loops in VBA)[2023-05-09]
试试这个: Sub matrix() Dim arr() As Variant Dim totrow As Long Dim j As Long Dim t As Long Dim p As Long Dim x As Long Dim ws As Worksheet Set ws = Sheets("Sheet1") 'Set your array of numbers arr = Array(2, 3, 4) 'If you want to refer to ranges on Sheet1 use: ... -
VBA中的双循环(Dual Loops in VBA)[2023-02-24]
正如您希望它们并行运行一样,您实际上不需要两个循环。 计算另一个变量: For j = 0 To 4 Step 1 i = j * 5 Range(ActiveCell, ActiveCell.Offset(5 + i, 0)).Copy Destination:=Cells(8, 12 + j) Next i As you want them to run in parallel, you actually don't want two loops. Calculate one variab ... -
VBA数组和循环(VBA Arrays and Loops)[2023-10-18]
我开始写一个大的描述,一个关于你的数组更大然后需要以及它们如何有一个零的基础,同时这样做我发现问题是你从来没有维度hiCustomer或hiSale 。 改变这个: - 'Loop through arrays to find sales over $500 With Range("A3") isOver = 0 For i = 1 To nCustomers If .Offset(i, 1).Value > 500 Then isOver = isOver ... -
VBA用循环转置数据(VBA Transpose data with loops)[2023-02-16]
是不是简单 Public Sub TEST() Dim myArr() Dim sourceRng As Range Set sourceRng = ActiveSheet.Range("A1:I9") myArr = sourceRng.Value Dim myArrTransposed() ReDim myArrTransposed(1 To UBound(myArr, 2), 1 To UBound(myArr, 1)) Dim i A ... -
没关系。 可以使用GoTo退出循环。 尽管使用GoTo跳转到'For'循环并不好。 (根据循环变量的数据类型,跳入,似乎会产生运行时错误,说'For'循环没有被初始化,或者默默无法循环。) 但是你不需要使用GoTo退出。 这正是Exit For语句的用途: If MsgBox("blah blah", vbYesNoCancel, "blah") = vbYes Then 'do stuff Exit For End If That's fine. It's okay to use GoTo ...
-
VBA DO循环问题(VBA DO Loops Issue)[2023-11-21]
为了更好地理解发生了什么,您需要以尽可能少的方式编写代码; 现在你有一个单一的程序,做了很多事情,很难确切地说出错的地方以及在哪里。 编写一个函数来确认用户的有效数字输入: Private Function ConfirmUserInput(ByVal input As Integer) As Boolean ConfirmUserInput = MsgBox("Confirm value: " & CStr(input) & "?", vbYesNo) = vbYes End Function ... -
For循环可用于获取具有相同索引的项目: AccountNumbers = Array("20T5555", "20T3333", "20T8888", "20T1111") AccountNames = Array("Branch 1", "Branch 2", "Branch 3", "Branch 4") For i = 0 To Ubound(AccountNames) Debug.Print AccountNumbers(i), AccountNames(i) Next i For l ...
-
VBA中的高效循环(Efficient Loops in VBA)[2020-01-27]
我将您的代码与我对excel的回答结合起来根据标准从表宏中删除行 ,我刚刚完成发布。 它超级快。 有关详细信息,请查看我的其他答案。 您可能需要调整Target范围。 如果您的数据在A1开始,并且没有任何完全空白的行,那么它应该起作用。 Sub DeleteRows() Dim Start: Start = Timer Application.ScreenUpdating = False Application.Calculation = xlCalculationManual ... -
VBA嵌套循环提前退出(VBA nested loops exiting early)[2022-04-05]
如果可以,我会发布此评论,但这太长了。 所以这里它是一个查询/潜在的解决方案 我认为您的范围参考是问题所在 下面的代码是代码的简化版本 curr_ep是curr_ep的命名范围。 它的范围地址为$Y$4:$AQ$58 当您遍历a变体时,您将使用此语法设置范围 a = curr_ep.Range(curr_ep.Cells(n - 3, 1), curr_ep.Cells(n - 3, 11)) 相当于a = curr_ep.Range("Y2:AQ2") 这意味着你实际上在看AW2:BG2而不是Y2:AQ2 ... -
表和循环VBA(Tables and loops VBA)[2024-02-24]
循环遍历范围内的每个单元格非常慢,因此您要么首先要将数据加载到数组中,要么使用常规Excel公式+ FillDown函数。 在这种特殊情况下,我建议使用第二个选项,它允许您将公式添加到单个单元格中并将其填充到列的其余部分。 像这样的东西应该工作: Dim colNum As Long With ThisWorkbook.Sheets("Example Sheet") 'Find last row in sheet Dim lastRow As Long lastRow = .Cell ...