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 Top answer 1 of 9
33
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
2 of 9
16
Not quite the same question but if you just want to show the top / bottom 5 rows (eg with display in jupyter or regular print, there's potentially a simpler way than this if you use the pd.option_context context.
#make 100 3d random numbers
df = pd.DataFrame(np.random.randn(100,3))
# sort them by their axis sum
df = df.loc[df.sum(axis=1).index]
with pd.option_context('display.max_rows',10):
print(df)
Outputs:
0 1 2
0 -0.649105 -0.413335 0.374872
1 3.390490 0.552708 -1.723864
2 -0.781308 -0.277342 -0.903127
3 0.433665 -1.125215 -0.290228
4 -2.028750 -0.083870 -0.094274
.. ... ... ...
95 0.443618 -1.473138 1.132161
96 -1.370215 -0.196425 -0.528401
97 1.062717 -0.997204 -1.666953
98 1.303512 0.699318 -0.863577
99 -0.109340 -1.330882 -1.455040
[100 rows x 3 columns]
Videos
02:52:03
pandas .head() to .tail() (Beginner) | SciPy 2018 Tutorial | ...
01:58:01
Tom Augspurger: Pandas: .head() to .tail() - YouTube
02:21
DataAnalysis with Python pandas - .head() and .tail() method of ...
04:35
Python Pandas Tutorial 6 - head and tail functions in Pandas - YouTube
07:43
Pandas head | Pandas tail | Pandas head and tail function in Python ...
08:58
Part 3 - Pandas DataFrame Functions | df.head() | df.tail ...
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
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
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.
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 ·
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
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 ...