Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a head and tail method for Numpy array?

Tags:

python

numpy

I loaded a csv file into 'dataset' and tried to execute dataset.head(), but it reports an error. How to check the head or tail of a numpy array? without specifying specific lines?

like image 926
ling Avatar asked Jan 19 '19 17:01

ling


People also ask

How many NumPy methods are there?

NumPy's operations are divided into three main categories: Fourier Transform and Shape Manipulation, Mathematical and Logical Operations, and Linear Algebra and Random Number Generation.


2 Answers

For a head-like function you can just slice the array using dataset[:10].

For a tail-like function you can just slice the array using dataset[-10:].

like image 182
feedMe Avatar answered Sep 23 '22 23:09

feedMe


You can do this for any python iterable.

PEP-3132 which is in python 3.x (https://www.python.org/dev/peps/pep-3132/) can use the * symbol for the 'rest' of the iterable.

To do what you want:

>>> import numpy as np >>> np.array((1,2,3)) array([1, 2, 3]) >>> head, *tail = np.array((1,2,3)) >>> head 1 >>> tail [2, 3] 
like image 35
Oliver Dudgeon Avatar answered Sep 22 '22 23:09

Oliver Dudgeon