How can I create a series of numbers increasing by a decimal in BigQuery? - sql

I'm trying to create a series of numbers for multiple samples using a distribution plot. I want to have numbers .01, .02, .03... etc. in a column by itself, just creating a random number.
I have tried randbetween(0,1) but I cannot have it run a specified number of times and it is also not in sequence. I also tried rand() but that didn't work either.
The output should be something like this:
0.000001
0.000002
0.000003
0.000004
..... etc

Use generate_array() to generate a series of numbers. Then divide:
select cast(n / 1000000 as numeric)
from unnest(generate_array(1, 10)) n

Related

Pandas run function only on subset of whole Dataframe

Lets say i have Dataframe, which has 200 values, prices for products. I want to run some operation on this dataframe, like calculate average price for last 10 prices.
The way i understand it, right now pandas will go through every single row and calculate average for each row. Ie first 9 rows will be Nan, then from 10-200, it would calculate average for each row.
My issue is that i need to do a lot of these calculations and performance is an issue. For that reason, i would want to run the average only on say on last 10 values (dont need more) from all values, while i want to keep those values in the dataframe. Ie i dont want to get rid of those values or create new Dataframe.
I just essentially want to do calculation on less data, so it is faster.
Is something like that possible? Hopefully the question is clear.
Building off Chicodelarose's answer, you can achieve this in a more "pandas-like" syntax.
Defining your df as follows, we get 200 prices up to within [0, 1000).
df = pd.DataFrame((np.random.rand(200) * 1000.).round(decimals=2), columns=["price"])
The bit you're looking for, though, would the following:
def add10(n: float) -> float:
"""An exceptionally simple function to demonstrate you can set
values, too.
"""
return n + 10
df["price"].iloc[-12:] = df["price"].iloc[-12:].apply(add10)
Of course, you can also use these selections to return something else without setting values, too.
>>> df["price"].iloc[-12:].mean().round(decimals=2)
309.63 # this will, of course, be different as we're using random numbers
The primary justification for this approach lies in the use of pandas tooling. Say you want to operate over a subset of your data with multiple columns, you simply need to adjust your .apply(...) to contain an axis parameter, as follows: .apply(fn, axis=1).
This becomes much more readable the longer you spend in pandas. 🙂
Given a dataframe like the following:
Price
0 197.45
1 59.30
2 131.63
3 127.22
4 35.22
.. ...
195 73.05
196 47.73
197 107.58
198 162.31
199 195.02
[200 rows x 1 columns]
Call the following to obtain the mean over the last n rows of the dataframe:
def mean_over_n_last_rows(df, n, colname):
return df.iloc[-n:][colname].mean().round(decimals=2)
print(mean_over_n_last_rows(df, 2, "Price"))
Output:
178.67

Conversion of MIPS to %

I've been learning TSQL and need some help with a conversion CPU MIPS into PERCENTAGE.
I've built my code to get some data that I'm expecting. In addition to this, I want to add a column to my code which is to get the CPU%. I have a column that gives me TOTALCPU MIPS and want to use this in the code but in the form of percentage. Example, I have these values in my TOTAL CPU Column:
1623453.66897
0
0
2148441.01573933
3048946.946314
I want to convert these values into percentage and use them. I couldn't find much info on the internet.
Appreciate your response.
I assume that you have 5 numeric quantities (2 of them being zero) and you want to find the percentage that corresponds to each of them out of the addition of the five quantities. Is it so?
To find the percentage of a particular number in the addition you multiply the number by 100 and divide by the addition, the result is the percentage that that number is in relation with the addition.
The sum: 6820841.631023
The percentage of the first number (of MIPS):
1623453.668970 * 100 / 6820841.631023 = 23.80136876 =>
23.80136876% is the percentage of CPU used by the first program.
To give the answer some SQL looking, refering to Mips_Table as the view/table that contains the MIPs data:
select mips, mips/totMips*100 Pct_CPU
from Mips_Table,
(select sum(mips) TotMips from Mips_Table) k

How to handle decimal numbers in solidity?

How to handle decimal numbers in solidity?
If you want to find the percentage of some amount and do some calculation on that number, how to do that?
Suppose I perform : 15 % of 45 and need to divide that value with 7 how to get the answer.
Please help. I have done research, but getting answer like it is not possible to do that calculation. Please help.
You have a few options. To just multiply by a percentage (but truncate to an integer result), 45 * 15 / 100 = 6 works well. (45 * 15%)
If you want to keep some more digits around, you can just scale everything up by, e.g., some exponent of 10. 4500 * 15 / 100 = 675 (i.e. 6.75 * 100).

error in dividing 2 pandas series with decimal values (daily stock price)

I am trying to divide 2 pandas columns (same column divided by shifting one cell) but getting an error as below...
..This is surprising as I have done such computation many times before on time series data and never encountered this issue.
Can someone suggest what is going on here?...I am computing the daily returns of Adj Close price of a stock so need the answer in decimal.
I think you need convert to float first column, because dtype is object, what is obviously string:
z = x.astype(float) / y.astype(float)
Or:
data['Adj Close'] = data['Adj Close'].astype(float)
z = data['Adj Close'].shift(-1) / data['Adj Close']

Generating Even Random Numbers

I need a code to generate only random EVEN numbers 2-100
There is tutorials on the web that generate random numbers but they're odd and even.
Please understand i just need even numbers to generate.
1, generate numbers 1-50
2, multiply all the numbers by 2
all numbers multiplied by 2 are even
This will work:
NSInteger evenNumber = (arc4random_uniform(50) + 1) * 2;
arc4random_uniform(50) will give results in the range 0 - 49. Adding 1 gives a value in the range 1 - 50. Multiplying by two gives you even numbers in the range 2 - 100.