This question already has answers here:
Translate integers in a numpy array to a contiguous range 0...n
(2 answers)
Closed 4 years ago.
I have a Numpy array with some numbers and I would like to get order the items ascending order.
For example, I have a list:
[4, 25, 100, 4, 50]
And I would like to use a function to get this:
[1, 2, 4, 1, 3]
Any ideas how to do this?
There is a convenient method via pandas:
import pandas as pd
lst = [4, 25, 100, 4, 50]
res = pd.factorize(lst, sort=True)[0] + 1
# [1 2 4 1 3]
Related
This question already has an answer here:
Cumsum as a new column in an existing Pandas data
(1 answer)
Closed 2 years ago.
Let sat I have a DataFrame with column A.
A= (1,2,3,4,5,6...n)
I want to create column B like this:
B=(1,3,6,10,15,21...n)
Explicitly: i+(sum of all the previous numbers)
Probably simple, but hard for me:P Very new to programming
Thanks!
from itertools import accumulate
A = [1, 2, 3, 4, 5, 6]
B = list(accumulate(A)) #->[1, 3, 6, 10, 15, 21]
This question already has answers here:
Find elements of array one nearest to elements of array two
(1 answer)
Find nearest indices for one array against all values in another array - Python / NumPy
(2 answers)
Closed 3 years ago.
Is there a numpy-thonic way to get the nearest values between two arrays:
Example:
a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
b = np.array([1.1, 3.2, 4.9, 7.2])
c = find_nearests(a, b)
c would be the size of b with the nearest elements from a:
print(c)
([1, 3, 5, 7])
This question is a direct reference to the following one that applies for searching one element in a tab: Find nearest value in numpy array
This question already has answers here:
How to conditionally combine two numpy arrays of the same shape
(2 answers)
Closed 5 years ago.
I am using numpy and I'm trying to compare between 2 arrays and get the higher value between them into a new array
arr1= array([1,2,3,4])
arr2= array([6,0,2,4])
newarr = array([6,2,3,4])
is there any way to do that
Yes, np.maximum:
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([6, 0, 2, 4])
newarr = np.maximum(arr1, arr2)
print(newarr)
>>> [6 2 3 4]
This question already has answers here:
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 6 years ago.
I have created a list size 12, ex: V[0 1 2 3 4 5 6 7 8 9 10 11]
How do I return only the last 10 digits [2:11]? Also what if the list had n variables, I tried V[2,:], but I get TypeError: list indices must be integers or slices, not tuple.
If you want to get the last x elements of a list, you need to use a negative index in your slice.
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[-5:]
[5, 6, 7, 8, 9]
As for the error you mentioned, it looks like you have a stray comma after the 2, which makes 2 the first element in a tuple. Take out the comma and it should be a valid slice.
This question already has answers here:
numpy.unique with order preserved
(7 answers)
Closed 6 years ago.
I have sequence of integer elements in array "a" ,given below
a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
I need the output like
output=[2,1,5,4,8,6,87,62,3]
I tried the built-in functions like set or unique but it arrange the
resulting sequence in ascending order, I want to keep the order unchanged.
Can anyone help?
You can use sorted with key=list.index
>>> a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
>>> new_a = sorted(set(a), key=a.index)
>>> new_a
[2, 1, 5, 4, 8, 6, 87, 62, 3]
a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
b = []
You can use
[b.append(x) for x in a if x not in b]
or easier to read
for x in a:
if x not in b:
b.append(x)
>>> [2, 1, 5, 4, 8, 6, 87, 62, 3]`