如何根据B列和C列中的值删除Excel中的行

时间:2017-02-11 16:43:29

标签: python excel vba pandas

我在下面的电子表格中有15,000行,我需要保留行:

其中status> 0和lastvalidationattemptdistance< 50

Spreadsheet

3 个答案:

答案 0 :(得分:2)

pandas中的简单数据操作。它非常简单实用,你可以在10分钟内学会它。

import pandas as pd

df = pd.read_excel('excel.xlsx', 'sheet_name', index_col=None, na_values=['NA'])

df = df.loc[df['status'] > 0]
df = df.loc[df['lastValidationAttemptDistance'] < 50]

writer = pd.ExcelWriter('new_execel.xlsx')
df.to_excel(writer, 'sheet_name', index=False)
writer.save()

答案 1 :(得分:1)

您可以在Excel中使用VBA删除所需内容,然后找到UserId。

Sub Delete()
'
' Delete and Find Macro
'


    Dim aRows As Integer, LVAD As Integer, Stat As Integer, UserId As Integer, UIDCount As Integer
    Dim Rng As Range, Rng2 As Range

    LVAD = 50 'Min value to keep
    Stat = 0 'Min value to keep
    UIDCount = 0 'Initial count number
    UserId = 3526 'Exact number of userId

    With ActiveSheet
    aRows = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    For i = 1 To aRows
        If Range("B" & i).Value <= 0 Then
            If Range("C" & i).Value > 50 Then
                If Rng Is Nothing Then
                    Set Rng = Range("A" & i & ":C" & i)
                Else
                    Set Rng2 = Range("A" & i & ":C" & i)
                    Set Rng = Application.Union(Rng, Rng2)
                End If
            End If
        End If
    Next

    For i = 1 To aRows
        If Range("A" & i).Value = UserId Then
            UIDCount = UIDCount + 1
        End If
    Next

    If Not Rng Is Nothing Then
        Rng.Select
        Selection.Delete Shift:=xlUp
    End If
    MsgBox "UserId: " & UserId & " was found " & UIDCount & " times."
End Sub

要为每个用户单独计算userId,您可以计算所有唯一ID,然后对每个用户进行循环迭代以计算出现次数,然后将这些值设置为列。

答案 2 :(得分:0)

由于您使用Excel for Windows,请考虑通过连接到Jet / ACE SQL引擎的SQL解决方案(通常安装在大多数PC上的Windows .dll文件和MS Access的引擎)。您可以在WHERE子句中使用您的确切条件。 For循环或嵌套If逻辑或公式。

下面假设数据驻留在名为DATA的工作表中,工作簿中存在一个名为RESULTS的空工作表,其中包含SQL查询的输出,包括标题。包括两种连接类型,即ODBC驱动程序和OLEDB提供程序。只需更改Excel数据文件的路径即可。

Public Sub RunSQL()
    Dim conn As Object, rst As Object
    Dim strConnection As String, strSQL As String
    Dim i As Integer

    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")

    ' DRIVER AND PROVIDER CONNECTION TYPES
'    strConnection = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" _
'                      & "DBQ=C:\Path\To\Workbook.xlsm;"
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
                       & "Data Source='C:\Path\To\Workbook.xlsm';" _
                       & "Extended Properties=""Excel 12.0;HDR=YES;"";"

    strSQL = " SELECT * FROM [DATA$]" _
                & " WHERE [status] > 0 AND [lastvalidationattemptdistance] < 50;"

    ' OPEN DB CONNECTION
    conn.Open strConnection
    rst.Open strSQL, conn

    ' COLUMN HEADERS
    For i = 1 To rst.Fields.Count
        Worksheets("RESULTS").Cells(1, i) = rst.Fields(i - 1).Name
    Next i    

    ' DATA ROWS
    Worksheets("RESULTS").Range("A2").CopyFromRecordset rst

    ' CLOSE OBJECTS AND FREE RESOURCES
    rst.Close: conn.Close
    Set rst = Nothing: Set conn = Nothing
End Sub