What's the best way to check if weight are between the range using the If condition?
Ex:
If textbox.text (between) value X - value Z then
You can use standard equal operators like this:
If (Val(TextBox.Text) >= ValueX) And (Val(TextBox.Text) <= ValueZ) Then
' etc...
Val function extracts numbers from string.
Related
Hello,
I am analyzing the next dataset with this information .
The column ['program_number'] is an object but I want to change it to a integer colum.
I have tried to replace some values but it doesn´t work.
as you can see, some values like 6 is duplicate. like '6 ' and 6.
How can I resolve it? Many thanks
UPDATE
Didn't see 1X and 3X at first.
If you need those numbers and just want to remove the X then:
df["Program"] = df["Program"].str.strip(" X").astype(int)
If there is data in the column which aren't numbers or which shouldn't be converted, you can use pd.to_numeric with errors='corece'. If there are cells which can't be converted, you'll get NaN. Be aware that this will result in floating numbers.
df["Program"] = pd.to_numeric(df["Program"], errors="coerce")
old
You want to use str.strip() here, rather than replace.
Try this:
df1['program_number'] = df1['program_number'].str.strip().astype(int)
I hope to filter on an array column to check whether every value in array is greater than 10 or smaller than 5, which is just like usage of cardinality(filter(col, x -> x < 5 or x >10)) > 0 in presto. Is it possible to achieve it with pure spark sql?
You can use forall function to achieve this
below is sample filter
df.filter(F.expr("forall(arr, x -> x<5 or x>10)"))
Here I am assuming arr is column name inside dataframe df where each value should either be less than 5 or greater than 10
Details of forall here
Task: I have data col(30) TYPE c VALUE '-1111,45'. and I need to check if this value is negative, if negative - do typecasting. But on the output I get 5 though I have to get 1111,45 without a minus and again do typecasting to the previous type (с).
REPORT Z_CAST.
data col(30) TYPE c VALUE '-1111,45'.
data numc type n.
numc = col.
if numc < 0.
numc = -1 * numc.
endif.
col = numc.
WRITE col. "Output: 5
REPORT Z_CAST.
data col(30) TYPE c VALUE '-1111,45'.
if col+0(1) EQ '-'.
WRITE col+1. "1111,45
endif.
This is the minimum character processing way to do this:
col = replace( val = col
sub = '-'
with = ' ' ).
You don't have to find the - first to replace it.
And this is the clean numeric way that ABAPers will usually choose:
DATA n TYPE p LENGTH 12 DECIMALS 2.
n = col.
n = abs( n ).
Note that this does not work with your example '-1111,45' though: ABAP expects a point . as decimal separator, but your number uses a localized format with comma ,, so you would have to normalize the number first.
mathematical range,for example:
greater or equal to 50 and smaller than 100 (>=50 && < 100)
smaller than 10 or greater than 40 (<10 || >40)
I have been thinking about how to represent mathematical range in a file and database, the range may be input by non programmer and I need to keep the input simple,but at another side, it also need to keep the input easy to convert to data and easy to check error input e.g.:"<10 || >100" seems the most simple but it is harder for me to parse the string to get the data,also need to consider input format error
I have been considering some input methods,using >=50 && < 100 as example,which is in key value form:
1.using 1 string to represent whole range:
<rangeInString>=50 && < 100</rangeInString>
2.separate 2 strings,one represent lower bound and another one represent upper bound,then parse each string in program:
<lowerBound> >=50 </lowerBound>
<upperBound> <100 </upperBound>
3.separate lower and upper bound,also separate the sign from number:
<lowerBound>
<sign> >= </sign>
<data>50</data>
</lowerBound>
<upperBound>
<sign> < </sign>
<data>100</data>
</upperBound>
4.separate lower bound and upper bound,also separate sign, and also separate the case that if includes the equal condition:
<lowerBound>
<sign> > </sign>
<isIncludeEqual>true</isIncludeEqual>
<data>50</data>
</lowerBound>
<upperBound>
<sign> < </sign>
<isIncludeEqual>false</isIncludeEqual>
<data>100</data>
</upperBound>
5.auto detect using "&&" or "||",e.g.:>= A with < B,if A < B,must be "&&" e.g.(>= 50 && <100),otherwise it is "||" e.g.(>= 100 || <50):
<A>
<sign> > </sign>
<isIncludeEqual>true</isIncludeEqual>
<data>50</data>
</A>
<B>
<sign> < </sign>
<isIncludeEqual>false</isIncludeEqual>
<data>100</data>
</B>
6.use a field "isAnd" to separate >=50 && < 100 (true) and <=50 || > 100 (false)instead of using field sign "<" and ">" :
<lowerBound>
<isIncludeEqual>true</isIncludeEqual>
<data>50</data>
</lowerBound>
<upperBound>
<isIncludeEqual>false</isIncludeEqual>
<data>100</data>
</upperBound>
<isAnd>true</isAnd>
7.other data model...
I need to consider somethings:
1.easy for non programmer to input
2.easy to convert or parse to data into program
3.easy to check error ,for example,parse string increase the complexity of converting data and checking incorrect format,also there may have other incorrect format,e.g.:<=50 && >100 should not be valid, I may allow auto detect using "&&" or "||" by the sign of input,but it may increase the complexity of the code
can anyone have idea?
Why "encode" it? There's no benefit or need and some hassle to use it.
Just store the exclusive range end values
low_end int,
high_end int,
You can then convert these raw values to useable expressions either in SQL or application code. You don't need to consider inclusive values because "n exclusive" === "n inclusive - 1" for low end and "n exclusive" === "n inclusive + 1" for high end.
Here's an SQL implementation:
where (low_end is null or col > low_end)
and (high_end is null or col < high_end)
If the range end values need to be floating point numbers, you'll need a little more:
low_end int,
low_inclusive boolean,
high_end int,
high_inclusive boolean,
And more code:
where (low_end is null or col > low_end + case when low_inclusive then 0 else 1 end)
and (high_end is null or col < high_end - case when high_inclusive then 0 else 1 end)
This is a good question, what about a combination of interval notation as suggested by Gordon and a given character for infinity. This combined with separate fields (or a parsing algorithm) could accomplish the task of defining any range.
For example, the range (3<x<20) could be written as (3,20). The range (x<=10 || x>30) could be written as the combination of
(-_,10],(30,_).
Where _ represents infinity. Or use the actual Infinity symbol character, ∞, Unicode U+221E.
This way would be pretty clear for those with a mathematics background, I believe, and would provide infinite flexibility.
I hope you find this helpful.
PostgreSQL does ranges natively.
The representation looks like this:
[low, high)
[ or ] = inclusive
( or ) = exclusive
Unbounded looks like this: [low-value, infinity]
http://www.postgresql.org/docs/9.4/static/rangetypes.html
Specifically addressing your options:
Why represent it in a format that you have to parse? A case could be made that you store it in a format that your code can parse, but what if you need to access it with a different programming language?
Same as 1.
Getting close, but you would need to subsume the bounds within a range object that includes && or ||. Also, no need for element, which is implied by "lower" and "upper" and could be replaced by an inclusive flag like you have in 4.
No need for
Unnecessary abstraction...it's just a range
That could work
Other data model:
The data is structured, so could work in json, xml, relational, or even as a set of semantic triples.
Ok so I am trying to reference one variable with another in SQL.
X= a,b,c,d (x is a string variable with a list of things in it)
Y= b ( Y is a string variable that may or may not have a vaue that appears in X)
I tried this:
Case when Y in (X) then 1 else 0 end as aa
But it doesnt work since it looks for exact matches between X and Y
also tried this:
where contains(X,#Y)
but i cant create Y globally since it is a variable that changes in each row of the table.( x also changes)
A solution in SAS would also be useful.
Thanks
Maybe like will help
select
*
from
t
where
X like ('%'+Y+'%')
or
select
case when (X like ('%'+Y+'%')) then 1 else 0 end
from
t
SQLFiddle example
In SAS I would use the INDEX function, either in a data step or proc sql. This returns the position within the string in which it finds the character(s), or zero if there is no match. Therefore a test if the value returned is greater than zero will result in a binary 1:0 output. You need to use the compress function with the variable containing the search characters as SAS pads the value with blanks.
Data step solution :
aa=index(x,compress(y))>0;
Proc Sql solution :
index(x,compress(y))>0 as aa