slice

How to split an array using its minimum entry

How to split an array using its minimum entry Question: I am trying to split a dataset into two separate ones by finding its minimum point in the first column. I have used idxmin to firstly identify the location of the minimum entry and secondly iloc to slice the array from 0 to the minimum …

Total answers: 1

slice(start, stop, None) vs slice(start, stop, 1)

slice(start, stop, None) vs slice(start, stop, 1) Question: I was surprised to read here that The start and step arguments default to None since it also says: slice(start, stop, step=1) Return a slice object representing the set of indices specified by range(start, stop, step). So I expected the default argument value for the step parameter …

Total answers: 1

How to use regular expression to replace split() in python

How to use regular expression to replace split() in python Question: i have a simple function which takes a s3 uri as input, and extracts its bucket name and key: def fun(s3_uri): bucket = s3_uri.split("/")[2] key = "/".join(s3_uri.split("/")[3:]) return bucket, key My question is: this clearly works, but what if the given s3_uri doesn’t have …

Total answers: 1

pandas column-slices with mypy

pandas column-slices with mypy Question: Lately I’ve found myself in a strange situation I cannot solve for myself: Consider this MWE: import pandas import numpy as np data = pandas.DataFrame(np.random.rand(10, 5), columns=list("abcde")) observations = data.loc[:, :"c"] features = data.loc[:, "c":] print(data) print(observations) print(features) According to this Answer the slicing itself is done correct and it …

Total answers: 1

Slice a multidimensional pytorch tensor based on values in other tensors

Slice a multidimensional pytorch tensor based on values in other tensors Question: I have 4 PyTorch tensors: data of shape (l, m, n) a of shape (k,) and datatype long b of shape (k,) and datatype long c of shape (k,) and datatype long I want to slice the tensor data such that it picks …

Total answers: 2

Slice and extract specific values from JSON output

Slice and extract specific values from JSON output Question: I am querying a REST API and I need to select 2 fields from the adapter output below, the query will include multiple incident numbers. I basically need a list of Incident Numbers and Descriptions from the output below. Code to get the data: headers = …

Total answers: 2

Assign to a whole column of a Pandas DataFrame with MultiIndex?

Assign to a whole column of a Pandas DataFrame with MultiIndex? Question: I have a DataFrame(called midx_df) with a MultiIndex, I want to assign values from a whole column of another DataFrame(called sour_df) with single level index to midx_df. All of index values of sour_df exist in the top level index of midx_df, I need …

Total answers: 1

Understanding Pytorch Tensor Slicing

Understanding Pytorch Tensor Slicing Question: Let a and b be two PyTorch tensors with a.shape=[A,3] and b.shape=[B,3]. Further b is of type long. Then I know there are several ways slicing a. For example, c = a[N1:N2:jump,[0,2]] # N1<N2<A would return c.shape = [2,2] for N1=1 and N2=4 and jump=2. But the below should have …

Total answers: 2

Set variable column values to nan based on row condition

Set variable column values to nan based on row condition Question: I want to be able to variably change a column value based on the value of the first column. Say I have a dataframe as follows: col_ind col_1 col_2 col_3 3 a b c 2 d e f 1 g h i I effectively …

Total answers: 4

Need to place multiple orders for stocks

Need to place multiple orders for stocks Question: I need to code in python for placing orders for stocks. Each order cannot exceed a certain quantity, say 600. If the required quantity is 1900, I would need to place three orders, 3 of 600 each and 1 of the remaining. I tried a for loop: …

Total answers: 2