I have 2 arrays, one of shape (455,98) and a second with shape (182,472). A geometric description is as per the attached image. Is there a pythonic way to do this? I would also be happy to receive guidance on how to write a function to achieve this. Answer Don’t know if I understood your question completely. However this code
Tag: indexing
Numpy assignment of 3D array values using 2D index array
I have a 3D numpy array of zeros, with dimensions CxHxW (in this example, C=4, H=2, and W=3): I also have a 2D array of indices, with dimensions HxW, such that every value in the array is a valid index between [0, C-1] Is there a fast way, using vectorization, to modify array A such that A[B[i][j]][i][j] = 1, for
What does numpy.ix_() function do and what is the output used for?
Below shows the output from numpy.ix_() function. What is the use of the output? It’s structure is quite unique. Answer According to numpy doc: Construct an open mesh from multiple sequences. This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with
How to get index of the max element from two lists after sorting it?
I have three lists price1, price2 and deviation. I wanted to find the top three highest price between price1 and price2. So to achieve that I first sorted them in decreasing order then I took the first 3 elements from both the list price1 and price2, and after that, I found the max between each max. Now I want to
Python Numpy; difference between colon and ellipsis indexing
I have been experimenting with Numpy array indexing using both colon and ellipsis. However, I cannot understand the results that I am getting. Below is the example code: Answer The original is (2,2) With :, it becomes (2,1,2). The new axis added after the first dimension. With … the shape is (2,2,1), the new shape is added last.
Datetime object in DataFrame with just time
This is the plain column And the I would like to put that column in the index, the problem is when I try to use the method resample() I always get the same problem: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of ‘Index’ I’ve been using this to change the Time column to Answer You
Python: concatenate pandas multiindex
I need to generate a pd.DataFrame with columns being composed by a list and a Multiindex object, and I need to do it before filling the final dataframe with data. Say the columns are [‘one’, ‘two’] and the multiindex obtained from from_product: I would like to get a list of columns which looks like this: One possible solution would be
How to move a column in a pandas dataframe
I want to take a column indexed ‘length’ and make it my second column. It currently exists as the 5th column. I have tried: I see the following error: TypeError: must be str, not list I’m not sure how to interpret this error because it actually should be a list, right? Also, is there a general method to move any
How can I extract the nth row of a pandas data frame as a pandas data frame?
Suppose a Pandas dataframe looks like: How can I extract the third row (as row3) as a pandas dataframe? In other words, row3.shape should be (1,5) and row3.head() should be: Answer Use .iloc with double brackets to extract a DataFrame, or single brackets to pull out a Series. This extends to other forms of DataFrame indexing as well, namely .loc
Filtering multiple items in a multi-index Python Panda dataframe
I have the following table: Note: Both NSRCODE and PBL_AWI are indices. How do I search for values in column PBL_AWI? For example I want to keep the values [‘Lake’, ‘River’, ‘Upland’]. Answer You can get_level_values in conjunction with Boolean slicing. The same idea can be expressed in many different ways, such as df[df.index.get_level_values(‘PBL_AWI’).isin([‘Lake’, ‘River’, ‘Upland’])] Note that you have