Skip to main content

Questions tagged [numpy]

NumPy is one of the many modules in Python that adds support of large multidimensional arrays and matrixes, along with a large library of high-level mathematical functions for operations with these arrays.

500 votes
14 answers
847k views

How do I create a new column where the values are selected based on an existing column?

How do I add a color column to the following dataframe so that color='green' if Set == 'Z', and color='red' otherwise? Type Set 1 A Z 2 B Z 3 B X 4 C Y
user7289's user avatar
  • 33.7k
369 votes
27 answers
429k views

Split (explode) pandas dataframe string entry to separate rows

I have a pandas dataframe in which one column of text strings contains comma-separated values. I want to split each CSV field and create a new row per entry (assume that CSV are clean and need only be ...
Vincent's user avatar
  • 17.3k
408 votes
11 answers
1.8m views

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Let x be a NumPy array. The following: (x > 1) and (x < 3) Gives the error message: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() How ...
Homunculus Reticulli's user avatar
214 votes
18 answers
127k views

Cartesian product of x and y array points into single array of 2D points

I have two numpy arrays that define the x and y axes of a grid. For example: x = numpy.array([1,2,3]) y = numpy.array([4,5]) I'd like to generate the Cartesian product of these arrays to generate: ...
Rich's user avatar
  • 12.5k
260 votes
20 answers
183k views

Find unique rows in numpy.array

I need to find unique rows in a numpy.array. For example: >>> a # I have array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [...
Akavall's user avatar
  • 85.2k
688 votes
11 answers
1.1m views

Most efficient way to map function over numpy array

What is the most efficient way to map a function over a numpy array? I am currently doing: import numpy as np x = np.array([1, 2, 3, 4, 5]) # Obtain array of square of each element in x squarer = ...
Ryan's user avatar
  • 8,091
707 votes
16 answers
1.7m views

Convert pandas dataframe to NumPy array

How do I convert a pandas dataframe into a NumPy array? DataFrame: import numpy as np import pandas as pd index = [1, 2, 3, 4, 5, 6, 7] a = [np.nan, np.nan, np.nan, 0.1, 0.1, 0.1, 0.1] b = [0.2, np....
Mister Nobody's user avatar
631 votes
8 answers
1.3m views

Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas

I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd_AKNatv, ERI_Asian, ERI_Black_Afr.Amer, ERI_HI_PacIsl, ERI_White) in each row of my ...
Dave's user avatar
  • 7,280
570 votes
7 answers
253k views

What are the advantages of NumPy over regular Python lists?

What are the advantages of NumPy over regular Python lists? I have approximately 100 financial markets series, and I am going to create a cube array of 100x100x100 = 1 million cells. I will be ...
Thomas Browne's user avatar
215 votes
12 answers
1.2m views

python numpy ValueError: operands could not be broadcast together with shapes

In numpy, I have two "arrays", X is (m,n) and y is a vector (n,1) using X*y I am getting the error ValueError: operands could not be broadcast together with shapes (97,2) (2,1) When (97,2)x(2,...
yayu's user avatar
  • 7,986
201 votes
4 answers
273k views

Binning a column with pandas

I have a data frame column with numeric values: df['percentage'].head() 46.5 44.2 100.0 42.12 I want to see the column as bin counts: bins = [0, 1, 5, 10, 25, 50, 100] How can I get the result as ...
Night Walker's user avatar
  • 21.1k
17 votes
2 answers
5k views

Python: Justifying NumPy array

Please I am a bit new to Python and it has been nice, I could comment that python is very sexy till I needed to shift content of a 4x4 matrix which I want to use in building a 2048 game demo of the ...
Akins Nazri's user avatar
211 votes
10 answers
265k views

Using NumPy to build an array of all combinations of two arrays

I'm trying to run over the parameters space of a six-parameter function to study its numerical behavior before trying to do anything complex with it, so I'm searching for an efficient way to do this. ...
Rafael S. Calsaverini's user avatar
800 votes
26 answers
1.5m views

How can the Euclidean distance be calculated with NumPy?

I have two points in 3D space: a = (ax, ay, az) b = (bx, by, bz) I want to calculate the distance between them: dist = sqrt((ax-bx)^2 + (ay-by)^2 + (az-bz)^2) How do I do this with NumPy? I have: ...
Nathan Fellman's user avatar
140 votes
9 answers
86k views

How do you fix "runtimeError: package fails to pass a sanity check" for numpy and pandas?

This is the error I am getting and, as far as I can tell, there is nothing useful on the error link to fix this. RuntimeError: The current Numpy installation ('...\\venv\\lib\\site-packages\\numpy\\...
Reed Collins's user avatar
  • 1,403

15 30 50 per page
1
2 3 4 5
���
884