Put Last Two Digits Behind Decimal - decimal

I have a column in SQL that uses an integer data type. What is the best way to get the values from the column where the last two digits of the integer are a decimal. These results would need to be displayed in a new column.
Example:
COL_1 Col_2
25489 -> 254.89
489196 -> 4891.96
250 -> 2.50
77 -> .77

It's easier than you think. Simply cast the value as a float and multiply it by 0.01. that will create a new value with the decimal two over from the right.

You just need to divide it by 100:
UPDATE my_table
SET col2 = col1 / 100.0

Related

Select k rows with the highest value of a given column

Let's suppose you have a pandas dataframe with col1 and you want to keep only the k samples with the highest value of col1. How can you do that?
Notice I'm not saying maximum value. But rather like sorting by col1, keeping the best k samples, and removing the rest.
k=10 # some number
df.sort_values('col', ascending=False).head(k)

MS excel calculate sum by conditions

I have a table:
col1
col2
134
1
432
2
222
3
21
4
982
5
1352
8
111
9
I need to find all possible sum combinations of col1 values IF col2 sum is 10. (5+4+1, 2+3+5, etc.) & number of terms is 3
Please advice how to solve this task?
To get all unique possible sums based on a give count of items in col2 and sum of col2 is a specific amount, with ms365, try:
Formula in D1:
=LET(inp,B1:B7,cnt,3,sm,10,B,COUNTA(inp),A,MAKEARRAY(B,cnt,LAMBDA(r,c,INDEX(inp,r,1))),D,B^cnt,E,UNIQUE(MAKEARRAY(D,cnt,LAMBDA(rw,cl,INDEX(IF(A="","",A),MOD(CEILING(rw/(D/(B^cl)),1)-1,B)+1,cl)))),F,FILTER(E,MMULT(--(E<>""),SEQUENCE(cnt,,,0))=cnt),G,FILTER(F,BYROW(F,LAMBDA(a,(SUM(a)=sm)*(COUNT(UNIQUE(a,1))=cnt)))),UNIQUE(BYROW(G,LAMBDA(a,SUM(XLOOKUP(a,inp7,A1:A7))))))
You can now change parameters cnt and sm to whichever amount you like.
The trick is borrowed from my answer here to calculate all permutations first. Instead of a range, the single column input is extended using MAKEARRAY().
A short visual representation of what is happening:
Expand the given array based on cnt;
Create a complete list of all possible permutations;
Filter step 2 based on a sum per row and unique numbers (don't use values from col2 more than once);
Lookup each value per row to create a total sum per row;
Return only the unique summations as per screenshot at the top.

Product MarkUp with Excel Formula

I have a list of product price that i need to markup.
I've tried using with Regular Formula
=A1*10%+A1+5,000 = 36,130 (A1 = 28,300)
Is there any way to remove the last 2 Digit and adjust the price if the price result is 36,130 it will remove the 30 (36,100) but if the price 36,160 it will adjust to 36,200 ?
Use:
=MROUND(<YourExpression>,100)
In order to round up to a certain number of digits, this is what I usually do (example of 2 digits):
multiply by 100
round to the nearest integer
divide by 100 again
(use 1000 for 3 digits, ...)
For rounding, you might use the ROUND() worksheet function.

Removing ".00" from Decimal format numbers to make them whole numbers keeping other decimal numbers intact

I have a column that has Amount rounded to 2 decimal places. So there are two types of enteries, one of the form 359.00 and others 359.78. I want to remove .00 from the first form of enteries to make it interger(359) keeping the decimal format of other number intact.
Data are floating numbers
A column don't have mixed types. Assuming your data are stored as floating numbers (dtype: float64), they will remain floats. What you can do is to use a custom format so that they are shown on the screen as you wish, but internally they remains floats. For example:
dfa = pd.DataFrame.from_records([(1,), (3.34,), (2.49,), (5,), (7,)], columns=['Amount'])
pd.options.display.float_format = lambda x : "{:2.2f}".format(x).rstrip('0').rstrip('.')
print(dfa)
This prints:
Amount
0 1
1 3.34
2 2.49
3 5
4 7
Data are strings
If instead your data are just strings representing numbers (dtype: object) you can use pandas.applymap to actually edit the strings according to a format style.
dfb = pd.DataFrame.from_records([("1.00",), ("3.34",), ("2.49",), ("5.00",), ("7.00",)], columns=['Amount'])
dfbb = dfb.applymap(lambda x : str(x).rstrip('0').rstrip('.'))
print(dfbb)
This prints (again):
Amount
0 1
1 3.34
2 2.49
3 5
4 7
Data are floating numbers but you want strings
In this case you can combine the two methods:
dfc = dfa.applymap(lambda x : "{:2.2f}".format(x).rstrip('0').rstrip('.'))
print(dfc)
Starting from a dataframe with floating numbers, you end with a dataframe of formatted strings. It prints the same, no need to touch pandas format settings.
Credits to this answer for the basic idea.

subtract values from different lines

I have a csv file that I am reading in, I have a column of numerical strings and I'm trying to get the difference between the two subsequent rows. The numbers were in depths with "ft" following the values (ex. 4.23ft), I was able to get rid of the "ft" (ex. 4.230), but can't figure out how to assign the values so I can do the math.
depth = float(depth)
rate=0
'''Need to find a way to subtract next line from current line to find
rate of change over 15 minute period'''
for i, data in enumerate(depth):
d1=i
d2=i+1
while rate == 0:
rate = d1-d2
print(rate)
This gives me a TypeError of " 'float' object is not iterable".
when I have the "depth = float(depth)" line commented out, I only get -1 values, which I understand the issue there.
first few lines of raw data
first few lines of result data
second row first value minus second value equals first value in third row.
Since you have already stripped the "ft" part from your column and assuming you have converted the remaining part of the string to float type, I will jump into the next part directly.
If I understand what you want to achieve correctly, you can also use pandas.DataFrame.shift:
df = pd.DataFrame()
df['D1'] = [1.0, 2.0, 3.0, 4.0, 5.0]
Your D1 the value from current row, D2 will be the column from D1 by performing shift operation.
df['D2'] = df['D1'].shift(-1)
Your dataframe will now look like:
D1 D2
0 1.0 2.0
1 2.0 3.0
2 3.0 4.0
3 4.0 5.0
4 5.0 NaN
In short, you have the values from the subsequent row of the current row into a new column. You can now perform subtraction/difference operation between the two columns as usual. For example:
df['D3'] = df['D1'] - df['D2']
or
df['D3'] = df['D1'].sub(df['D2'])

Resources