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:].

Answer from feedMe on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ .head() like method for numpy arrays
r/learnpython on Reddit: .head() like method for numpy arrays
October 10, 2023 -

Is there anything similar I can use to get a view of whatever data I'm looking at? Array slicing doesn't include the row and column labels, which kind of defeats the purpose. My teacher uses the shell command !head, but it seems to not work on windows.

Thank you in advance.

Discussions

pandas - What does !head do in python and NumPy? - Stack Overflow
It has nothing to do with Python or NumPy specifically; that's a feature of ipython to run external programs, in this case, the head utility from the standard suite of *NIX command line utilities. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Remove head and tail from numpy array PYTHON - Stack Overflow
I have a numpy.ndarray, and want to remove first h elements and last t. As I see, the more general way is by selecting: h, t = 1, 1 my_array = [0,1,2,3,4,5] middle = my_array[h:-t] and the middle... More on stackoverflow.com
๐ŸŒ stackoverflow.com
pandas - Easiest way to print the head of a data in python? - Stack Overflow
Bring the best of human thought ... Stack Internal ... I'm not defining my array with pandas, I'm using numpy to do it and I would like to know if there is any other way to print the first 5 rows of a data. Using pandas this is how I would do it: print(data.head()... More on stackoverflow.com
๐ŸŒ stackoverflow.com
df.head(n) - Why does it exist?
The df.head() function saves you 1 character and the name of it is more intuitive than self.iloc in term of getting the head of the data frame. More on reddit.com
๐ŸŒ r/learnpython
3
0
October 22, 2022
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ is-there-a-head-and-tail-method-for-numpy-array.aspx
Python - Is there a head and tail method for NumPy array?
# Import numpy import numpy as np # Creating a numpy array arr = np.array([1,2,3,4,5,6,7,8,9,10]) # Display original array print("Original array:\n",arr,"\n") # Getting first 5 elements of array head = arr[:5] # Display the result of head print("Head:\n",head,"\n") # Getting last 5 elements of array tail = arr[5:] # Display the result of tail print("Tail:\n",tail,"\n") ... Comments and Discussions! ... D.S. Programs ... Copyright ยฉ 2025 www.includehelp.com.
Top answer
1 of 2
2

Instead, use

middle = my_array[h:len(my_array)-t]

For completeness, here's the trial run:

my_array = [0,1,2,3,4,5]
h,t = 0,0
middle = my_array[h:len(my_array)-t]
print(middle)

Output: [0, 1, 2, 3, 4, 5]

This example was just for a standard array. Since your ultimate goal is to work with numpy multidimensional arrays, this problem is actually a bit trickier. When you say you want to remove the first h elements and the last t elements, are we guaranteed that h and t satisfy the proper divisibility criteria so that the result will be a well-formed array?

I actually think the cleanest solution is simply to use this solution, but divide out by the appropriate factor first. For example, in two dimensions:

h = 3
t = 6
a = numpy.array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])

d = numpy.prod(numpy.shape(a)[1:])
mid_a = a[int(h/3):int(len(a)-t/3)]
print(mid_a)

Output: array([[4, 5, 6]])

I have included the int casts in the indices because python 3 automatically promotes division to float, even when the numerator evenly divides the denominator.

2 of 2
1

The i:j can be replaced with a slice object. and ':j' with slice(None,j), etc:

In [55]: alist = [0,1,2,3,4,5]
In [56]: h,t=1,-1; alist[slice(h,t)]
Out[56]: [1, 2, 3, 4]
In [57]: h,t=None,-1; alist[slice(h,t)]
Out[57]: [0, 1, 2, 3, 4]
In [58]: h,t=None,None; alist[slice(h,t)]
Out[58]: [0, 1, 2, 3, 4, 5]

This works for lists and arrays. For multidimensional arrays use a tuple of indices, which can include slice objects

x[i:j, k:l]
x[(slice(i,j), Ellipsis, slice(k,l))]
๐ŸŒ
w3resource
w3resource.com โ€บ pandas โ€บ dataframe โ€บ dataframe-head.php
Pandas DataFrame: head() function - w3resource
August 19, 2022 - The head() function is used to get the first n rows. This function returns the first n rows for the object based on position. It is useful for quickly testing if your object has the right type of data in it.
๐ŸŒ
Medium
medium.com โ€บ @Shatriya โ€บ intro-to-pandas-and-numpy-532a2d5293c8
Intro to Pandas and Numpy: Basic Tutorials Part 6 | by Abhishek V Suryavanshi | Medium
March 19, 2018 - The head command is used to return the first N rows in the data frame whereas tail is used to get the last N rows. ... Dataframe is a 2-dimensional labeled data structure with columns of potentially different types. df = pd.DataFrame([[y, x1_1, ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.ndarray.view.html
numpy.ndarray.view โ€” NumPy v2.4 Manual
New view of array with the same data ยท Passing None for dtype is different from omitting the parameter, since the former invokes dtype(None) which is an alias for dtype('float64')
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ head-and-tail-function-in-python
Head and Tail Function in Python - Javatpoint
Head and Tail Function in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ head () and tail () functions explained with examples and codes
Head () and Tail () Functions Explained with Examples and Codes
June 11, 2025 - This article deeply delves into the head and tail functions, with illustrated code examples in Python, R, and other related programming languages, demonstrating their importance in diverse data analysis contexts.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.head.html
pandas.DataFrame.head โ€” pandas 3.0.2 documentation
Return the first n rows ยท This function exhibits the same behavior as df[:n], returning the first n rows based on position. It is useful for quickly checking if your object has the right type of data in it
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-37712.html
numpy.array has no attribute head
July 13, 2022 - I am trying to see the columns in a dataframe. I am using the following Python 3 code: scaler=StandardScaler() df=scaler.fit_transform(df) df.head()and I get the following error: Error:AttributeError Traceback (most rec...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ pandas โ€บ ref_df_head.asp
Pandas DataFrame head() Method
A DataFrame with headers and the specified number of rows. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python_pandas โ€บ python_pandas_basic_functionality.htm
Python Pandas - Basic Functionality
import pandas as pd import numpy as np # Create a Series with random numbers s = pd.Series(np.random.randn(10)) print("Series:") print(s) # Using basic methods print("First 5 elements of the Series:\n", s.head()) print("\nLast 3 elements of the Series:\n", s.tail(3)) print("\nDescriptive statistics of the Series:\n", s.describe())
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas head() โ€“ returns top n rows
Pandas head() - Returns Top N Rows - Spark By {Examples}
December 12, 2024 - # Syntax of head() method DataFrame.head(n) Series.head(n) ... # Create a pandas DataFrame import pandas as pd import numpy as np df = pd.DataFrame({ 'Courses' :['Spark','Python','Java','C++','Hadoop','R','C#','AWS'], 'Fee' :[22000,25000,23000,22000,30000,22000,32000,40000], 'Duration':['30days','50days','30days','35days','40days','45days','50days','60days'] }) print(df) # Output: # Courses Fee Duration # 0 Spark 22000 30days # 1 Python 25000 50days # 2 Java 23000 30days # 3 C++ 22000 35days # 4 Hadoop 30000 40days # 5 R 22000 45days # 6 C# 32000 50days # 7 AWS 40000 60days
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ df.head(n) - why does it exist?
r/learnpython on Reddit: df.head(n) - Why does it exist?
October 22, 2022 -

Can somebody please explain why the head() function in pandas exists. Since it only returns self.iloc[:n], why was it implemented in the first place?

I use df.head(n) all the time. But I just looked into the source code an realized that there is no "extra value" that is being added. Just a bunch of kinda redunant lines in the code. Is there a reason why it is not redundant?

Sorry for the nooby question and thanks to all serious replies :)

๐ŸŒ
Python Forum
python-forum.io โ€บ archive โ€บ index.php โ€บ thread-37712.html
Python Forum - numpy.array has no attribute head
July 13, 2022 - Error:AttributeError Traceback (most recent call last) Input In [11], in <cell line: 3>() 1 scaler=StandardScaler() 2 df=scaler.fit_transform(df) ----> 3 df.head() AttributeError: 'numpy.ndarray' object has no attribute 'head'I still want to see the columns in df.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-the-use-of-head-methods-in-pandas-series
What is the use of head () methods in Pandas series?
November 18, 2021 - This method takes an integer value as a parameter to return a series with those many rows, suppose if you give integer n as a parameter to the head method like head(n) then it will return a pandas series with n number of elements. And those elements are the first n number of elements of our ...
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ dev โ€บ reference โ€บ api โ€บ pandas.DataFrame.head.html
pandas documentation - DataFrame.head - PyData |
Return the first n rows ยท This function exhibits the same behavior as df[:n], returning the first n rows based on position. It is useful for quickly checking if your object has the right type of data in it