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.
numpy
114,586
questions
897
votes
23
answers
1.2m
views
How do I print the full NumPy array, without truncation?
When I print a numpy array, I get a truncated representation, but I want the full array.
>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(...
823
votes
21
answers
775k
views
How do I get indices of N maximum values in a NumPy array?
NumPy proposes a way to get the index of the maximum value of an array via np.argmax.
I would like a similar thing, but returning the indexes of the N maximum values.
For instance, if I have an array, ...
809
votes
13
answers
1.3m
views
Dump a NumPy array into a csv file
How do I dump a 2D NumPy array into a csv file in a human-readable format?
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:
...
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....
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 = ...
677
votes
12
answers
696k
views
What does -1 mean in numpy reshape?
A 2D array can be reshaped into a 1D array using .reshape(-1).
For example:
>>> a = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> a.reshape(-1)
array([[1, 2, 3, 4, 5, 6, 7, 8]])
...
676
votes
10
answers
1.2m
views
How do I access the ith column of a NumPy multidimensional array?
Given:
test = np.array([[1, 2], [3, 4], [5, 6]])
test[i] gives the ith row (e.g. [1, 2]). How do I access the ith column? (e.g. [1, 3, 5]). Also, would this be an expensive operation?
668
votes
24
answers
1.1m
views
Is there a NumPy function to return the first index of something in an array?
I know there is a method for a Python list to return the first index of something:
>>> xs = [1, 2, 3]
>>> xs.index(2)
1
Is there something like that for NumPy arrays?
651
votes
32
answers
1.1m
views
How do I count the occurrence of a certain item in an ndarray?
How do I count the number of 0s and 1s in the following array?
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
y.count(0) gives:
numpy.ndarray object has no attribute count
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 ...
582
votes
13
answers
769k
views
Pandas read_csv: low_memory and dtype options
df = pd.read_csv('somefile.csv')
...gives an error:
.../site-packages/pandas/io/parsers.py:1130:
DtypeWarning: Columns (4,5,7,16) have mixed types. Specify dtype
option on import or set low_memory=...
574
votes
8
answers
930k
views
How can I use the apply() function for a single column?
I have a pandas dataframe with multiple columns. I want to change the values of the only the first column without affecting the other columns. How can I do that using apply() in pandas?
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 ...
568
votes
14
answers
1.3m
views
How do I read CSV data into a record array in NumPy?
Is there a direct way to import the contents of a CSV file into a record array, just like how R's read.table(), read.delim(), and read.csv() import data into R dataframes?
Or should I use csv.reader() ...