 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Best algorithm to populate the minimum value of rows above in python

By : Filip Konečný
Date : October 17 2020, 08:10 AM
will be helpful for those in need I currently have a data frame i'd like to create a new column titled "min". Which stores the lowest number (found in 'num' column) of the rows above (including current row). Is there a built in API that does this? I can only think of creating a loop that loops through every prior row. , Provided this is your dataframe:(only a sample) code :
``````In : df
Out:
date   num
0  12/10/2018 13:56   400
1  12/10/2018 12:28   600
2  12/10/2018 13:57   300
3  12/10/2018 12:36  1200

In : df['min'] = df['num'].cummin()

In : df
Out:
date   num  min
0  12/10/2018 13:56   400  400
1  12/10/2018 12:28   600  400
2  12/10/2018 13:57   300  300
3  12/10/2018 12:36  1200  300
``````

## Minimum Distance Algorithm using GDAL and Python

By : user3038794
Date : March 29 2020, 07:55 AM
around this issue You should definitely be using NumPy. I work with some pretty large raster datasets and NumPy burns through them. On my machine, with the code below there's no noticeable delay for a 1000 x 1000 array. An explanation of how this works follows the code.
code :
``````import numpy as np
from scipy.spatial.distance import cdist

# some starter data
dim = (1000,1000)
values = np.random.randint(0, 10, dim)

# cdist will want 'samples' as a 2-d array
samples = np.array([1, 2, 3]).reshape(-1, 1)

# this could be a one-liner
# 'values' must have the same number of columns as 'samples'
mins = cdist(values.reshape(-1, 1), samples)
outvalues = mins.argmin(axis=1).reshape(dim)
``````

## Minimum removed nodes required to cut path from A to B algorithm in Python

By : user7427549
Date : March 29 2020, 07:55 AM
this will help Here is an answer which ignores the list of paths. It just takes a network, a source node, and a target node, and finds the minimum set of nodes within the network, not either source or target, so that removing these nodes disconnects the source from the target.
If I wanted to find the minimum set of edges, I could find out how just by searching for Max-Flow min-cut. Note that the Wikipedia article at http://en.wikipedia.org/wiki/Max-flow_min-cut_theorem#Generalized_max-flow_min-cut_theorem states that there is a generalized max-flow min-cut theorem which considers vertex capacity as well as edge capacity, which is at least encouraging. Note also that edge capacities are given as Cuv, where Cuv is the maximum capacity from u to v. In the diagram they seem to be drawn as u/v. So the edge capacity in the forward direction can be different from the edge capacity in the backward direction.

## Python - convert rows to columns after group by and populate zeroes for non matching rows

By : Paulina
Date : March 29 2020, 07:55 AM
wish of those help What you need to use is pivot_table from pandas. You can specify what rows and columns you need, fill_value states what do you want to do with empty values and aggfunc len counts.
I'm not sure what your DataSeries looks like, but you need sth like this:
code :
``````pd.pivot_table(data, index='user_id', columns='type', aggfunc=len, fill_value=0)
``````

## Prim's algorithm for minimum spanning trees - confusion in algorithm

By : user3828926
Date : March 29 2020, 07:55 AM
With these it helps One of the guys at MO was kind enough to answer by email. The problem was that I didn't notice that the tree nodes are added one at a time via the ExtractMin(Q) operation.
Here is the reply he gave:

## krukshal's algorithm or Prims Algorithm which one is better in finding minimum spanning tree?

By : y3gang
Date : March 29 2020, 07:55 AM
I hope this helps . I'll add one point in favour of Prim's algorithm I haven't seen mentioned. If you are given N points and a distance function d(x,y) for the distance between x and y, it is easy to implement Prim's algorithm using space O(N) (but time N^2).
Start off with an arbitrary point A and create an array of size N-1 giving you the distances from A to all other points. Pick the point, B, associated with the shortest distance, link A and B in the spanning tree and then update the distances in the array to be the minimum of the distance already noted down to that other point and the distance from B ot that other point, noting down where the shortest link is from B and where from A. Carry on. 