Skip to main content
Filter by
Sorted by
Tagged with
925 votes
23 answers
1.3m views

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(...
kame's user avatar
  • 22.3k
827 votes
13 answers
1.4m views

How do I dump a 2D NumPy array into a csv file in a human-readable format?
Dexter's user avatar
  • 11.9k
718 votes
17 answers
1.7m views

How do I convert a Pandas dataframe into a NumPy array? import numpy as np import pandas as pd df = pd.DataFrame( { 'A': [np.nan, np.nan, np.nan, 0.1, 0.1, 0.1, 0.1], 'B': [0.2, ...
Mister Nobody's user avatar
695 votes
12 answers
727k views

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]]) ...
user2262504's user avatar
  • 7,417
678 votes
32 answers
1.2m views

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
mflowww's user avatar
  • 7,515
598 votes
14 answers
837k views

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=...
Josh's user avatar
  • 12.9k
580 votes
14 answers
1.3m views

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() ...
hatmatrix's user avatar
  • 45.5k
520 votes
16 answers
566k views

How do I sort a NumPy array by its nth column? For example, given: a = array([[9, 2, 3], [4, 5, 6], [7, 0, 5]]) I want to sort the rows of a by the second column to obtain: ...
user avatar
510 votes
15 answers
870k views

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
  • 34.6k
499 votes
21 answers
630k views

How do I find the nearest value in a numpy array? Example: np.find_nearest(array, value)
Fookatchu's user avatar
  • 7,475
494 votes
14 answers
674k views

How do I print formatted NumPy arrays in a way similar to this: x = 1.23456 print('%.3f' % x) If I want to print the numpy.ndarray of floats, it prints several decimals, often in 'scientific' format, ...
camillio's user avatar
  • 4,955
480 votes
17 answers
595k views

After creating a NumPy array, and saving it as a Django context variable, I receive the following error when loading the webpage: array([ 0, 239, 479, 717, 952, 1192, 1432, 1667], dtype=int64) ...
Karnivaurus's user avatar
  • 24.4k
465 votes
9 answers
807k views

What is the simplest way to compare two NumPy arrays for equality (where equality is defined as: A = B iff for all indices i: A[i] == B[i])? Simply using == gives me a boolean array: >>> ...
clstaudt's user avatar
  • 22.7k
460 votes
4 answers
146k views

import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the ...
cryptomanic's user avatar
  • 6,346
450 votes
10 answers
1.1m views

How do I get the dimensions of an array? For instance, this is 2x2: a = np.array([[1, 2], [3, 4]])
Cristi P's user avatar
  • 6,241
447 votes
24 answers
1.0m views

I have a matrix in the type of a Numpy array. How would I write it to disk it as an image? Any format works (png, jpeg, bmp...). One important constraint is that PIL is not present.
M456's user avatar
  • 5,857
444 votes
7 answers
719k views

How do I convert a NumPy array into a Python List?
Alex Brooks's user avatar
  • 5,453
432 votes
3 answers
278k views

I am trying to implement a "Digit Recognition OCR" in OpenCV-Python (cv2). It is just for learning purposes. I would like to learn both KNearest and SVM features in OpenCV. I have 100 samples (i.e. ...
Abid Rahman K's user avatar
421 votes
11 answers
1.9m views

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
405 votes
17 answers
588k views

How do I efficiently obtain the frequency count for each unique value in a NumPy array? >>> x = np.array([1,1,1,2,2,2,5,25,1,1]) >>> freq_count(x) [(1, 5), (2, 3), (5, 1), (25, 1)]
Abe's user avatar
  • 24.2k
402 votes
10 answers
588k views

How do I drop nan, inf, and -inf values from a DataFrame without resetting mode.use_inf_as_null? Can I tell dropna to include inf in its definition of missing values so that the following works? df....
user avatar
396 votes
13 answers
510k views

If I have a numpy dtype, how do I automatically convert it to its closest python data type? For example, numpy.float32 -> "python float" numpy.float64 -> "python float" numpy.uint32 -> "...
conradlee's user avatar
  • 13.9k
391 votes
17 answers
763k views

How can I check which version of NumPy I'm using?
larus's user avatar
  • 4,587
379 votes
8 answers
202k views

How does np.einsum work? Given arrays A and B, their matrix multiplication followed by transpose is computed using (A @ B).T, or equivalently, using: np.einsum("ij, jk -> ki", A, B)
Lance Strait's user avatar
  • 4,321
374 votes
27 answers
446k views

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
  • 18.3k

1
2 3 4 5
2306