大熊猫滚动计数

时间:2020-11-03 17:52:21

标签: python pandas

对此有一些答案,但仍然无法获得正确的结果。计算先前的结果中有多少大于或小于当前结果:

df = pd.DataFrame({"B": [0, 1, 2, 1, 0]})

较大的应该返回:

[0, 0, 0, 1, 3]

较小的应该返回:

 [0, 1, 2, 1, 0]

2 个答案:

答案 0 :(得分:1)

让我们尝试[WebBrowser].ScriptErrorsSuppressed = True广播

Private Sub btnNavigate_Click(sender As Object, e As EventArgs) Handles btnNavigate.Click
    AddHandler webBrowser1.DocumentCompleted, AddressOf Browser_DocumentCompleted
    webBrowser1.Navigate("https://SomeAddress.com")
End Sub

Private Sub Browser_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    Dim browser = DirectCast(sender, WebBrowser)
    If browser.ReadyState <> WebBrowserReadyState.Complete Then Return

    ' Select a Form with a specific className
    Dim form = browser.Document.Forms.OfType(Of HtmlElement).FirstOrDefault(Function(frm) frm.GetAttribute("className").StartsWith("form-login"))

    ' Not found at this point, try later
    If form Is Nothing Then Return

    ' Select the element by ID or by className or whatever
    Dim userId = form.Document.GetElementById("[The UserId Input's ID]")
    ' Same for other input elements
    Dim userPwd = form.Document.GetElementById("[The Password Input's ID]")
    If userId Is Nothing OrElse userPwd Is Nothing Then Return

    ' Set the value of both Input elements. Note that a validation procedure 
    ' may require that you set both the Value and the InnerText
    userId.SetAttribute("value", "[The User LogIn ID]")
    'userId.InnerText = "[The User LogIn ID]"
    userPwd.SetAttribute("value", "[The User LogIn Password]")
    'userPwd.InnerText = "[The User LogIn Password]"

    ' Filter (fail-safe) the SUBMIT button by className, since - here - it has no ID
    Dim submit = browser.Document.GetElementsByTagName("button").OfType(Of HtmlElement).
        FirstOrDefault(Function(elm) elm.GetAttribute("type").
            Equals("submit") AndAlso elm.GetAttribute("className").Contains("bt-label"))

    ' The SUBMIT Button was found: click it. Also remove the handler: we're done here
    ' The WebBrowser is redirected to a landing page, this event is no longer needed
    If submit IsNot Nothing Then
        RemoveHandler browser.DocumentCompleted, AddressOf Browser_DocumentCompleted
        submit.InvokeMember("click")
    End If
End Sub

答案 1 :(得分:1)

您可以使用numpy进行此操作:

In [226]: import numpy as np

In [227]: larger = np.sum(np.tril(np.subtract.outer(df.B.values,df.B.values), k=0)<0, axis=1)

In [228]: larger
Out[228]: array([0, 0, 0, 1, 3])

In [233]: smaller = np.sum(np.tril(np.subtract(df.B.values, df.B.values[:, None])) < 0, 1)

In [234]: smaller
Out[234]: array([0, 1, 2, 1, 0])

或者如果您不想使用numpy ,请执行以下操作:

In [245]: larger = [(df.B[x]<df.B[:x]).sum() for x in range(len(df.B))]

In [246]: smaller = [(df.B[x]>df.B[:x]).sum() for x in range(len(df.B))]

In [247]: larger
Out[247]: [0, 0, 0, 1, 3]

In [248]: smaller
Out[248]: [0, 1, 2, 1, 0]