Skip to content

Tag: numpy

Check if two numpy arrays are identical

Suppose I have a bunch of arrays, including x and y, and I want to check if they’re equal. Generally, I can just use np.all(x == y) (barring some dumb corner cases which I’m ignoring now). However this evaluates the entire array of (x == y), which is usually not needed. My arrays are really large,…

Numpy diff inverted operation?

Working with numpy.diff function, suppose this simple case: How can I get easily x back to original scale not differenced? I suppose there is something with numpy.cumsum(). Answer Concatenate with the first element and then use cumsum – For concatenating, we can also use np.hstack, like so – Or wi…

check if two numeric values have same sign in numpy (+/-)

currently i am using numpy.logical_or with numpy.logical_and to check if elements of two arrays have same sign. Was wondering if there is already a ufunc or a more effective method that will achieve this. My current solutions is here edit// output Answer One approach with elementwise product and then check fo…

pandas DataFrame style, highlight nan’s

Say with this DataFrame How can I check which element is nan inside df.applymap? (ie, not using df.isnull) The problem comes from where I want to use the pandas html styling. We have the built-in nan highlighting but it changes the background colour, instead I want “nan” to be displayed in red. So…