I am trying to delete filtered rows using xlwings.
Ex.
I have filters like below
import xlwings as xw
filename = r"C:\Users\User\Xwlings\Input VAT_2021.12_rev.xlsx"
sht = xw.Book(filename)
sht.sheets['Vendor_code'].api.Range('A1:F1900').AutoFilter(Field:=3, Criterial:='TATA')
After filters like that, I want to delete the filtered rows.
Xlwings should delete the visible rows so after applying the filter, delete the range (from row 2 assuming headers) to the last row.
When the filter is removed those rows not matching the criteria should remain.
import xlwings as xw
filename = r"C:\Users\User\Xwlings\Input VAT_2021.12_rev.xlsx"
sht = xw.Book(filename)
sht.sheets['Vendor_code'].api.Range('A1:F1900').AutoFilter(Field:=3, Criterial:='TATA')
### Get the max row to delete to or use 1900.
last_row = sht.sheets['Vendor_code'].range(1,1).end('down').row
### Delete visible rows from 2 to the last row
sht.sheets['Vendor_code'].range("2:" + str(last_row)).delete()
### Remove the filter and only the rows hidden by the filter should now be left
sht.sheets['Sheet2'].api.AutoFilterMode=False
Related
If I have created a Dates column in flask sqlalchemy and also stored some dates in it, how can I check if each and every one of these dates are between to dates that I choose
There are lots of ways to accomplish this. Here's one example.
The goal is to select all rows that have a date outside your desired range. If the result set is empty, all rows have a valid date. For good measure, we'll include any rows that don't have a date value in our "bad rows" query.
from sqlalchemy import select, or_
with Session.begin() as session:
my_start_date = '2022-01-01'
my_end_date = '2022-01-31'
query = select(MyTable).where(
or_(MyTable.date < my_start_date,
MyTable.date > my_end_date,
MyTable.date == Null)
)
results = session.execute(query).all()
Now you can take a look at the results and see what's up.
you can use between in your orm or plain query:
with Session.begin() as session:
my_start_date = '2022-01-01'
my_end_date = '2022-01-31'
q = session.query(table_name).filter(table_name.c.date.between(my_start_date,my_end_date))
.....
or
select(table_name).where(table_name.c.date.between(my_start_date, my_end_date))
I have a database table that contains records, where each record has a criteria attribute. This criteria attribute can hold anywhere between 1-n criteria that I'd like to apply as filters on a different table.
This looks something like:
message.status:::eq:::submitted;;;message.count:::ge:::5
but could also be only
message.count:::ge:::5
What I'd like to do in DAX, is take that string and translate it into dynamic Filter attributes. So I somehow need to split the string based on ;;;, and then disect each section into the target (e.g. message[count]), the operator (e.g. ge --> >=) and the value (e.g. 5).
So in the end the following DAX snippet should be added to my Calculate 1 or more times:
example measure = CALCULATE(
COUNTROWS('message),
FILTER (
ALL('message'),
--- line below should be dynamically injected
message[count] >= 5
),
I'm struggling with how to create a loop (is this even possible in PBI?), and then even with a single string... hoe to filter based on this.
Thanks
You can try to build new table for splited message.
Table 2 =
var _splitby = ";;;"
var _string = SELECTCOLUMNS(ADDCOLUMNS(VALUES('Table'[message]),"pathx", SUBSTITUTE([message],_splitby,"|")),"pathx",[pathx])
var _generate = GENERATE(_string, GENERATESERIES(1, PATHLENGTH([pathx])))
var _GetVal = SELECTCOLUMNS(_generate, "Msg", PATHITEM([pathx], [Value]))
return
_GetVal
If you have always message.count:::ge::: like string at the end, you can follow these steps in Power Query-
Step 1: Duplicate you message column
Step-2: apply split on new duplicated column using string message.count:::ge::: and you will have a new column with last Numeric value from your original text.
Step-3: you can now apply filter on the new column.
Sample Output-
I am trying to move the rows with NULL values in all 4 columns DOB, Address1, address2 and Postcode to a new data frame and keep original datafarme with clean records
i have tried solving it by using the following code
import numpy as np
import pandas as pd
BadRecords = Data.dropna(subset=['DOB','Address1','Address2','PostCode'], how='any')
print(BadRecords)
The current code is printing the entire dataset. It should only filter the records where DOB, Address1, Address2 and postcode all 4 are NULLs
To get records with null values you can filter the original set like this:
from pyspark.sql.functions import col, isnull
badRecords = Data.filter(isnull(col('DOB')) & isnull(col('Address1')) & isnull(col('Address2')) & isnull(col('PostCode')))
display(badRecords)
dropna function returns a new dataframe omitting rows with null values, so with that you can only get "good" records
goodRecords = Data.dropna(subset=['DOB','Address1','Address2','PostCode'], how='all')
Also notice that how='any' will drop rows with at least one of values null, so if you want to filter rows only when all of them are null, you need to use 'all' setting.
I have three column one is Id(ID is same) 2nd col is amount and third is date, I want difference between two rows(amount)
As you want to have the previous value of the date where the ID is equal, you can use the following:
Add a column,
Column4 =
var baseFilter = FILTER(DiffRows;DiffRows[Column1] = EARLIER(DiffRows[Column1]))
var selectDate = CALCULATE(LASTDATE(DiffRows[Column3]);baseFilter;
FILTER(baseFilter; DiffRows[Column3] < EARLIER(DiffRows[Column3])))
return
DiffRows[Column2] - CALCULATE(sum(DiffRows[Column2]);baseFilter;
FILTER(baseFilter; DiffRows[Column3] =selectDate))
First I create a basefilter to ensure the IDs are same.
Next I select the date whcih is the previousdate within the set of same ids
Last I use this date, to filter the correct value out of the rows.
End result:
How to remove the entire blank row from the existing Excel Sheet using Python?
I need a solution which DOES NOT :
Include reading the whole file and rewriting it without the deleted row.
IS THERE ANY DIRECT SOLUTION?
I achieved using Pandas package....
import pandas as pd
#Read from Excel
xl= pd.ExcelFile("test.xls")
#Parsing Excel Sheet to DataFrame
dfs = xl.parse(xl.sheet_names[0])
#Update DataFrame as per requirement
#(Here Removing the row from DataFrame having blank value in "Name" column)
dfs = dfs[dfs['Name'] != '']
#Updating the excel sheet with the updated DataFrame
dfs.to_excel("test.xls",sheet_name='Sheet1',index=False)
If using memory is not an issue you can achieve this using an extra dataframe.
import pandas as pd
#Read from Excel
xl= pd.ExcelFile("test.xls")
dfs = xl.parse(xl.sheet_names[0])
df1 = dfs[dfs['Sal'] == 1000]
df1 = df1[df1['Message']=="abc"]
ph_no = df1['Number']
print df1
To delete an Excel row, say row 5, column does not matter so 1:
sh.Cells(5,1).EntireRow.Delete()
To delete a range of Excel rows, say row 5 to 20
sh.Range(sh.Cells(5,1),sh.Cells(20,1)).EntireRow.Delete()