You can use iloc with numpy.r_:

print (np.r_[0:2, -2:0])
[ 0  1 -2 -1]

df = df.iloc[np.r_[0:2, -2:0]]
print (df)
            A  B  C
2012-11-29  0  0  0
2012-11-30  1  1  1
2012-12-07  8  8  8
2012-12-08  9  9  9

df = df.iloc[np.r_[0:4, -4:0]]
print (df)
            A  B  C
2012-11-29  0  0  0
2012-11-30  1  1  1
2012-12-01  2  2  2
2012-12-02  3  3  3
2012-12-05  6  6  6
2012-12-06  7  7  7
2012-12-07  8  8  8
2012-12-08  9  9  9
Answer from jezrael on Stack Overflow
🌐
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 - Both functions are used to get ... analysis in Python. head and tail: The two functions work together to give a complete picture of the dataset from the beginning and end, respectively....
🌐
w3resource
w3resource.com › pandas › head-and-tail.php
Pandas: Head and Tail - w3resource
Pandas Head and Tail: To view a small sample of a Series or DataFrame object, use the head() and tail() methods. The default number of elements to display is five, but you may pass a custom number.
🌐
AskPython
askpython.com › home › get head and tail of a pandas dataframe or series
Get Head and Tail of a Pandas Dataframe or Series - AskPython
February 16, 2023 - Last 10 values of the sample pandas Series: 990 -0.239336 991 -1.475996 992 -0.162860 993 0.405505 994 0.458872 995 1.186127 996 -0.898278 997 -0.267392 998 1.295608 999 -2.024564 dtype: float64 · In this Python tutorial, we have learned how to get the head and tail of a pandas DataFrame or Series using the head() and tail() functions.
🌐
TutorialsPoint
tutorialspoint.com › python_pandas › python_pandas_basic_functionality.htm
Python Pandas - Basic Functionality
Let us now create a Series and see the working of the Series basic methods. 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()) Its output is as follows − ·
🌐
PYnative
pynative.com › home › python › pandas › pandas dataframe head, tail, at, iat
Pandas DataFrame head, tail, at, iat
March 9, 2023 - Like the head function, this function is used when we want to view a smaller section of the entire DataFrame. It takes input as the number of rows to be displayed from the bottom. The default value is 5. ... In the below Student DataFrame with columns like Name, Age, and Marks. If we apply DataFrame.tail() function, we can see that only the bottom five rows are displayed in the output. import pandas as pd student_dict = {'Name': ['Joe', 'Nat', 'Harry','Jack','Jose',"Jill","Rose"], 'Age': [20, 21, 19,17,18,19,17], 'Marks': [85.10, 77.80, 91.54,72,87.9,90,72]} # create DataFrame from dict student_df = pd.DataFrame(student_dict) # display the bottom 5 rows bottomRows = student_df.tail() print(bottomRows)Code language: Python (python) Run
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Get first/last n rows of DataFrame with head() and tail() | note.nkmk.me
January 25, 2024 - By default, the first 5 rows are ... print(df.head(3)) # col_0 col_1 # row_0 A 9 # row_1 B 8 # row_2 C 7 ... The tail() method returns the last n rows....
Find elsewhere
🌐
Pythontic
pythontic.com › pandas › dataframe-manipulations › head-tail
Retrieve elements from the head and tail of a pandas DataFrame | Pythontic.com
The head() method selects first 'n' elements from a DataFrame instance. Similarly, the tail() method of the DataFrame class selects the last 'n' number of rows.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 18691
Add an ends function that shows both head and tail of the df · Issue #18691 · pandas-dev/pandas
December 8, 2017 - Add the following function to pd. dataFrame and pd.Series def ends(df, x=5): """Returns both head and tail of the dataframe or series. Args: x (int): Optional number of rows to return for each head and tail """ print('{} rows x {} column...
Author   icfly2
🌐
Educative
educative.io › answers › what-are-head-and-tail-functions-in-pandas
What are head and tail functions in pandas?
The code snippet below shows how the head function is used in pandas: ... The tail function in Python displays the last five rows of the dataframe by default. It takes in a single parameter: the number of rows.
🌐
DataScience Made Simple
datasciencemadesimple.com › home › head and tail function in python pandas (get first n rows & last n rows)
Head and tail function in Python pandas (Get First N Rows & Last N Rows) - DataScience Made Simple
February 26, 2023 - Head and Tail function in python pandas. head() function gets first n rows and tail() function returns last (few) n rows of data frame pandas example head()
🌐
Plantsandpython
plantsandpython.github.io › PlantsAndPython › G_4_DATA_ANALYSIS_WITH_PANDAS › 0_Lessons › 4.1_Head_and_Tail.html
4.1 Head and Tail — Plants & Python
# It's important to know what you are working with # to "see" the dataframe # .head() shows the first rows and column names data.head() We can also use tail. And tail, if the head is at the beginning, then the tail is the end. You can see that we get the last rows in our dataset.
🌐
Softhints
softhints.com › how-to-select-rows-with-head-and-tail-in-pandas
How to Select Rows with Head and Tail in Pandas - Softhints
October 7, 2020 - You may use Pandas functions head() and tail() in order to show first and last N rows of a DataFrame simultaneously. Let's review several ways to combine head() and tail functionality.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.tail.html
pandas.DataFrame.tail — pandas 3.0.2 documentation
DataFrame.head · The first n rows of the caller object. Examples · >>> df = pd.DataFrame( ... { ... "animal": [ ... "alligator", ... "bee", ... "falcon", ... "lion", ... "monkey", ... "parrot", ... "shark", ... "whale", ... "zebra", ... ] ... } ... ) >>> df animal 0 alligator 1 bee 2 falcon 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra · Viewing the last 5 lines · >>> df.tail() animal 4 monkey 5 parrot 6 shark 7 whale 8 zebra ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › select-first-or-last-n-rows-in-a-dataframe-using-head-and-tail-method-in-python-pandas
Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas - GeeksforGeeks
July 15, 2025 - # display top 2 rows of the specific ... using tail() method of Pandas DataFrame : Pandas tail() method is used to return bottom n (5 by default) rows of a data frame or series....
🌐
Finxter
blog.finxter.com › pandas-dataframe-head-method
Pandas DataFrame head() and tail() Method – Be on the Right Side of Change
May 5, 2022 - The head() method returns the top five (5) rows of the DataFrame. The tail() method returns the bottom five (5) rows of the DataFrame
🌐
Saturn Cloud
saturncloud.io › blog › how-to-select-both-head-and-tail-in-python-pandas
How to Select Both Head and Tail in Python Pandas | Saturn Cloud Blog
September 9, 2023 - We have learned that the head() function can be used to select the first few rows of a dataset, while the tail() function can be used to select the last few rows of a dataset. We have also learned that the concat() function can be used to ...
🌐
APXML
apxml.com › courses › intro-eda-course › chapter-2-data-loading-inspection-cleaning › initial-data-look
First Look at the Data: Shape, Head, Tail
Using shape, head(), and tail() together provides a quick, essential overview of your dataset's dimensions and a preview of its contents. This initial inspection is a simple but significant step in familiarizing yourself with the data before moving on to more detailed analysis like examining ...