Calculate sliding window median for Pandas df

Advertisements This is the follow-up question to link So, I have the following df: df = pd.DataFrame({‘col1’:[1,2,3,4,5,6,7,8,9,10], ‘col2’:[10,15,20,25,30,35,40,45,50,55], ‘col3’:[‘A’,’A’,’A’,’A’,’B’,’B’,’B’,’B’,’B’,’B’]}) I would like to calculate the median values for col1 and col2 for every ‘window’ rows. To do that I have the following function: def calculate_median_val(df, window): return df.groupby(np.arange(len(df))//window)[‘col1′,’col2’].median() There are two problems with this function:… Read More Calculate sliding window median for Pandas df

What is the "peak to peak" analogy refrenced by numpy's .ptp function?

Advertisements The np.ptp function returns the range between minimum and maximum values along a specified axis. The numpy docs state that the "ptp" name is an acronym for "peak to peak". Can someone explain this analogy? I would have thought this function would have been named "valley to peak". >Solution : This is a common… Read More What is the "peak to peak" analogy refrenced by numpy's .ptp function?

Making an array from lists of different sizes and filling the gaps with Nan in Python

Advertisements Let’s say I have a list of lists, and they can have different sizes: arr=list([[0. , 1.5, 3. , 0. , 1.5],[0. , 1.5, 3. ],[0., 1.33, 2.67, 4. ]]) I want to make this array numpy compatible and make a filled array based on the maximum length and fill the gaps with Nan:… Read More Making an array from lists of different sizes and filling the gaps with Nan in Python

Joining two multi-dimentional arrays by adding a new dimention in Python

Advertisements Let’s say I have two arrays of shape (3,2,3) a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]) b = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]) a.shape b.shape I would like to join these two arrays by adding a new dimention to get (2,3,2,3) like this: c = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]]) c.shape How would I do this? >Solution : You can use numpy.stack (Join a sequence… Read More Joining two multi-dimentional arrays by adding a new dimention in Python