How can I use .startswith to replace the ENTIRE value of the cell instead of just the matching string? - pyspark

I'm trying to do something to the effect of this:
Name
Status
Bill
Cancelled on 01/01/2023
Jim
Active
Kim
Servicing
Rod
Cancelled on 05/01/1999
Tii
Active
Meg
Cancelled without cause
DF2= DF1.withColumn("status", when(DF1.status.startswith("Cancelled"), replace(DF1.status, "Eliminate")).otherwise(DF1.status))
Name
Status
Bill
Eliminate
Jim
Active
Kim
Servicing
Rod
Eliminate
Tii
Active
Meg
Eliminate
I'm not sure if what I wrote is right, but that end result is what I'm looking. I know regexp_replace() but that will only replace the word "cancel" and not the whole value. I know there has to be a quicker way than creating a UDF.
DF2= DF1.withColumn("status", when(DF1.status.startswith("Cancelled"), replace(DF1.status, "Eliminate")).otherwise(DF1.status))
(invalid syntax)

The correct function is when, no need for replace.
from pyspark.sql.functions import when
DF2= DF1.withColumn("status", when(DF1.Status.startswith("Cancelled"), "Eliminate").otherwise(DF1.Status))

Related

Getting the word the appeears after a specific word in flutter

I have this string.
String message = 'QG935XSVSD Confirmed. Your account balance was: M-PESA Account : Ksh0.00 on 9/7/22 at 12:33 PM. Transaction cost, Ksh0.00. Dial *334# now to get your stamped M-Pesa Statement.'
I want to get the word that appears right after cost, in this case Ksh0.00 . I would try and pick the word by its position but the message might get additional words affecting its length.
If you are sure about the string structure, simplest solution would probably be .split() method.
final cost = message.split('cost, ')[1].split(' ')[0];

In flutter TextFormField I can't clear the value when using double.parse

I am creating a dialog that allows a user to make a payment and it subtracts it from their current balance.
So if their balance is $100 and they type $25 into the form field and hit submit the balance is now $75.
That all works. My problem is if I erase everything in the TextFormField and hit submit the $100 balance is now $98.
That's because when I erase the value the first character sticks in the value and I can't delete it by hitting the delete key.
When I print the value, hit the delete key, and look at the console it looks like this...
25.00
25.0
25.
25
2
No matter how many times I hit delete, the 2 won't erase.
I narrowed it down to double.parse being my problem. Here is the code:
onChanged: (val){balance = debtBalance-double.parse(val);
print(val);},```
I think this is because parse method fails when provided with empty or null value. It doesn't really make sense to parse an empty string anyways, so just use default value 0 in those cases.
balance = debtBalance - ((val ?? '').isEmpty ? 0 : double.parse(val));
This code snippet checks if new text value is empty, and if so it substracts 0 instead of parsing.

How to reach the end of a chain of cells

I have a file with two sheets:
sheet_A
A B
1 Mr. Joe USD
sheet_B
A B
1 =sheet_A.A1 ???
sheet_B.B1 shall show the value USD. I know i could get it easily with =sheet_A.B1 but I do not want that.
If I enter into sheet_B.B1 =ADDRESS(ROW();COLUMN()-1) I get the output $C$1 and with =INDIRECT(ADDRESS(ROW();COLUMN()-1)) Mr. Joe.
How can I "navigate" through a chain sheet_B.B1 - sheet_B.A1 - sheet_A.A1 - sheet_A.B1?
Edit 1
Maybe I need something like this
=OFFSET(FORMULA(ADDRESS(ROW();COLUMN()-1);0;1)#
sheet_B.B2 shall show the content of sheet_A.B2 in relation of the value in sheet_B.A1
Here are two possibilities. Either formula produces USD in sheet_B.B1.
=INDIRECT(ADDRESS(ROW();COLUMN();;;"sheet_A"))
=VLOOKUP(A1;$sheet_A.A1:B1;2)
Documentation: ADDRESS, VLOOKUP.
EDIT:
One more idea: The following produces the string "=sheet_A.A1", which could be parsed for the sheet name and cell address. Perhaps you would like to use it to refer to sheet_A.B1.
=FORMULA(INDIRECT(ADDRESS(ROW();COLUMN()-1)))
However, as I commented, there is probably an easier way for what you are trying to accomplish.
Documentation: FORMULA.
EDIT 2:
Here is the formula you requested. It uses REPLACE to remove = at the beginning of the string.
=OFFSET(INDIRECT(REPLACE(FORMULA(INDIRECT(ADDRESS(ROW();COLUMN()-1)));1;1;""));0;1)

TSQL Find and Replace in String

I have a table that I use for error logging when inserts fail on the front end of my site. It stores the params URL as a text string so we can see what values were sent over and why it may have failed.
Well I am now working with this data to try and recover some records from it.
This is what the record looks like in my field:
xml=<data><optional><Account>192070041</Account></optional></data>, submitter=Q1370, target=Q1234, escalationType=esc, escalationReason=277, feedback=cx req live esc to have us release his alh payment for 8487.18, adv cx his funds are eligble for release on july 2nd at 445 pm est, preventable=0,
The issue I am running into recovering some data is that on a script I am writing in PHP, I am getting all of the params individualy by exploding on the = sign to get each of the values.
Well, the feedback= section happens to be comments that contains commas and its messing up a lot of stuff.
What I need to do is within the string, I need to find everything in feedback=xxxxxxxxxx, and either remove all the commas from that section or replace with with a | pipe so I can just change them back later.
My lack of knowledge in this area is where I hope some one can point me in the right direction so I can get some records restored on a mass level.
Example:
Before String - param1=dfsfsf, param2=fdsfsdfds, param3=bob, how are you doing today?
After String - param1=dfsfsf, param2=fdsfsdfds, param3=bob| how are you doing today?
UPDATE YourTable SET URL=REPLACE(URL,',','|')
See https://msdn.microsoft.com/en-us/library/ms186862.aspx and https://msdn.microsoft.com/en-us/library/ms181984.aspx
Later edit: I read your question more carefully and I now understand that you want to replace only the commas in after a certain substring. Try something like this:
DECLARE #URL NVARCHAR(1000)
SET #URL='Before String - param1=dfsfsf, param2=fdsfsdfds, param3=bob, how are you doing today?'
SELECT LEFT(#URL,ISNULL(NULLIF(CHARINDEX('param3=',#URL),0),LEN(#URL)))
+ISNULL(REPLACE(SUBSTRING(#URL,NULLIF(CHARINDEX('param3=',#URL),0),1000),',','|'),'')

Having issues with a substring function in Tableau

I am trying to break a string up into substrings based on the position of a repeating set of characters.
The source string [UPDATES] looks like this, the number of characters between the repeating portions varies wildly.
"04/24/15 15:12:54 (PZPJ3F): Task update. 04/24/15 15:12:54 (PZPJ3F): Task update. 04/22/15 15:17:13 (SZGQ3T): updated due date prior to global problem 04/22/15 12:28:09 (PZPJ3F): Task updates."
I am trying to break them up into separate substrings so that I can display them side by side as separate columns as below
Column1 = |04/24/15 15:12:54 (PZPJ3F): Task update.
Column2 = |04/24/15 15:12:54 (PZPJ3F): Task update.
Column3 = |04/22/15 15:17:13 (SZGQ3T): updated due date prior to global problem|
I got the first portion to work with
LEFT([UPDATES],FIND([UPDATES],"): ",28)-27)
But my attempts at using FIND to locate the next occurrence of "): " and use it to begin a MID are not working, specifically where I try to end them using a FIND function.
IF [Mark1]>0 THEN MID([UPDATES],[Mark1]-25,[Mark2])
ELSE ""
END
Where Mark1 is
FLOAT(FIND([UPDATES],"): ",(FIND([UPDATES],"): ")+1)))
and Mark2 is
FLOAT(FIND([UPDATES],") ",[Mark1]+1))
I really went down the rabbit hole at the end of my attempt.
I am using Tableau 8.2, so Tableau 9 functions aren't an option (looking forward to FIND Nth!
Thanks in advance.
The key is that find() takes a second optional argument as a start position.
So in Tableau 8.2, I would write a simple calc to find the position of the first separator. Then reference that calculated field twice in your final calculated fields to yield the length of the first substring and the starting point of the second one.
Separating out substrings is painful prior to Tableau 9. Extracting the first in a list isn't bad, getting the second is clumsy and after that it gets pretty ugly.
Best approach is to upgrade to version 9 or do some preprocessing to pull out the substrings.