loop in json_extract_scalar - sql

The json format I have is in the following form
{"0": {...}, "1":{...}, "2":{..}}
and this goes for a variable number of integers.
I am trying to create a for loop so I can extract all the values at once instead of separately writing a bunch of code with json_extract_scalar.
I wanted to do something like the following
for x in range (0, (some_number)):
json_extract_scalar(my_dataset_column, '$.' + x)

Related

Is there an equivalent of an f-string in Google Sheets?

I am making a portfolio tracker in Google Sheets and wanted to know if there is a way to link the "TICKER" column with the code in the "PRICE" column that is used to pull JSON data from Coin Gecko. I was wondering if there was an f-string like there is in Python where you can insert a variable into the string itself. Ergo, every time the Ticker column is updated the coin id will be updated within the API request string. Essentially, string interpolation
For example:
TICKER PRICE
BTC =importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids={BTC}","0.current_price")
You could use CONCATENATE for this:
https://support.google.com/docs/answer/3094123?hl=en
CONCATENATE function
Appends strings to one another.
Sample Usage
CONCATENATE("Welcome", " ", "to", " ", "Sheets!")
CONCATENATE(A1,A2,A3)
CONCATENATE(A2:B7)
Syntax
CONCATENATE(string1, [string2, ...])
string1 - The initial string.
string2 ... - [ OPTIONAL ] - Additional strings to append in sequence.
Notes
When a range with both width and height greater than 1 is specified, cell values are appended across rows rather than down columns. That is, CONCATENATE(A2:B7) is equivalent to CONCATENATE(A2,B2,A3,B3, ... , A7,B7).
See Also
SPLIT: Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
JOIN: Concatenates the elements of one or more one-dimensional arrays using a specified delimiter.

Pinescript index range

Is there a way to code an index range in pinescript? For example, If I want to include all close values between 10 bars and 5 bars ago. Everything between close[10] and close[5].
In python this would be close[5:10] but I cannot find any literature discussing a range of indexes.
thanks!
You could code a function to do that, but to be clear [] has a specific meaning in Pinescript as a history-referencing operator. I think what you are asking for is a way to construct an array of values from a series based on indicies.
This would work if you're use float values like OHLC
//#version=4
study("My Script")
range(_src, _a, _b) =>
_arr = array.new_float(0)
for i = _a to _b - 1
array.push(_arr, _src[i])
_arr
someCloses = range(close, 5, 10)
plot(array.size(someCloses))
But with this you are converting your data to a different type. So make sure to look at the available array functions.

Iterate on OrientRecord object

I am trying to increment twice in a loop and print the OrientRecord Objects using Python.
Following is my code -
for items in iteritems:
x = items.oRecordData
print (x['attribute1'])
y=(next(items)).oRecordData #Here is the error
print (y['attribute2'])
Here, iteritems is a list of OrientRecord objects. I have to print attributes of two consecutive objects in one loop.
I am getting the following error -
TypeError: 'OrientRecord' object is not an iterator
Try using a different approach to it:
for i in range(0,len(iteritems),2):
x = iteritems[i].oRecordData
print (x['attribute1'])
y = iteritems[i+1].oRecordData
print (y['attribute2'])
The range() function will start from 0 and iterate by 2 steps.
However, this will work properly only if the total amount (range) of records is an even number, otherwise it'll return:
IndexError: list index out of range
I hope this helps.

Create new index using pandas by appending a comma

Currently the index in my data frame has the default values of 0,1,2 .. n-1 where n is the number of rows in the dataframe.
Is there a simple way to change the index values to 0,,1,,2,, ... n-1, where a comma is appended to each index value. So 0 becomes 0, and 1 becomes 1, and so on.
I'd keep it simple.
d.index = d.index.to_series().astype(str) + ','
I converted the existing index to a series so that I could conveniently add a comma to it. However I had to ensure it was of type str before I did so.
Sure, see below:
d = pd.DataFrame(...)
d.index = [str(i)+',' for i in d.index]
But what are you trying to do with this? It seems odd to modify the index like this. If you're trying to print the the data frame in a special format or something else there is probably a better way.
For custom output, you could do something like
for i, row in d.iterrows():
print i + ': ' + ', '.join(row)

Convert cell text in progressive number

I have written this SQL in PostgreSQL environment:
SELECT
ST_X("Position4326") AS lon,
ST_Y("Position4326") AS lat,
"Values"[4] AS ppe,
"Values"[5] AS speed,
"Date" AS "timestamp",
"SourceId" AS smartphone,
"Track" as session
FROM
"SingleData"
WHERE
"OsmLineId" = 44792088
AND
array_length("Values", 1) > 4
AND
"Values"[5] > 0
ORDER BY smartphone, session;
Now I have imported the result in Matlab and I have six vectors and one cell (because the text from the UUIDs was converted in cell) all of 5710x1 size.
Now I would like convert the text in the cell, in a progressive number, like 1, 2, 3... for each different session code.
In Excel it is easy with FIND.VERT(obj, matrix, col), but I do not know how do it in Matlab.
Now I have a big cell with a lot of codes like:
ff95465f-0593-43cb-b400-7d32942023e1
I would like convert this cell in an array of numbers where at the first occurrence of
ff95465f-0593-43cb-b400-7d32942023e1 -> 1
and so on. And you put 2 when a different code appear, and so on.
OK, I have solve.
I put the single session code in a second cell C.
At this point, with a for loop, I obtain:
%% Converting of the UUIDs into integer
C = unique(session);
N = length(session);
session2 = zeros(N, 1);
for i = 1:N
session2(i) = find(strcmp(C, session(i)));
end
Thanks to all!