首页 \ 问答 \ 使用.apply()来比较元素(use of .apply() for comparing elements)

使用.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
更新时间:2022-02-03 20:02

最满意答案

试试这个:

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.

相关问答

更多
  • 试试这个: 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: ...
  • 正如您希望它们并行运行一样,您实际上不需要两个循环。 计算另一个变量: 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 ...
  • 我开始写一个大的描述,一个关于你的数组更大然后需要以及它们如何有一个零的基础,同时这样做我发现问题是你从来没有维度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 ...
  • 是不是简单 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 ...
  • 为了更好地理解发生了什么,您需要以尽可能少的方式编写代码; 现在你有一个单一的程序,做了很多事情,很难确切地说出错的地方以及在哪里。 编写一个函数来确认用户的有效数字输入: 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 ...
  • 我将您的代码与我对excel的回答结合起来根据标准从表宏中删除行 ,我刚刚完成发布。 它超级快。 有关详细信息,请查看我的其他答案。 您可能需要调整Target范围。 如果您的数据在A1开始,并且没有任何完全空白的行,那么它应该起作用。 Sub DeleteRows() Dim Start: Start = Timer Application.ScreenUpdating = False Application.Calculation = xlCalculationManual ...
  • 如果可以,我会发布此评论,但这太长了。 所以这里它是一个查询/潜在的解决方案 我认为您的范围参考是问题所在 下面的代码是代码的简化版本 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 ...
  • 循环遍历范围内的每个单元格非常慢,因此您要么首先要将数据加载到数组中,要么使用常规Excel公式+ FillDown函数。 在这种特殊情况下,我建议使用第二个选项,它允许您将公式添加到单个单元格中并将其填充到列的其余部分。 像这样的东西应该工作: Dim colNum As Long With ThisWorkbook.Sheets("Example Sheet") 'Find last row in sheet Dim lastRow As Long lastRow = .Cell ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)