如何根据列B和C中的值删除Excel中的行
我在下面的电子表格中有15,000行,我需要保留行:
其中status> 0,lastvalidationattemptdistance <50
简单的数据操作在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()
你可以在Excel中使用VBA删除你需要的,然后find一个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,然后为每个用户进行循环迭代来计算出现次数,然后可以将这些值设置为列。
由于您使用的是Windows版Excel,因此请考虑连接到Jet / ACE SQL引擎(通常安装在大多数PC上的Windows .dll文件以及MS Access的引擎)以提供SQL解决scheme。 您可以在WHERE
子句中使用您的确切条件。 否循环或嵌套If
需要逻辑或公式。
下面假定数据驻留在名为DATA
的工作表中,并在工作簿中存在一个名为RESULTS
的空工作表,该工作表将包含包括标题的SQL查询的输出。 ODBC驱动程序和OLEDB提供程序包括两种连接types。 只需将path更改为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