Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.tail.html
pandas.DataFrame.tail — pandas 3.0.2 documentation
>>> df.tail() animal 4 monkey 5 parrot 6 shark 7 whale 8 zebra · Viewing the last n lines (three in this case) >>> df.tail(3) animal 6 shark 7 whale 8 zebra · For negative values of n · >>> df.tail(-3) animal 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra ·
W3Schools
w3schools.com › python › pandas › ref_df_tail.asp
Pandas DataFrame tail() Method
In this example we use a .csv file called data.csv · import pandas as pd df = pd.read_csv('data.csv') print(df.tail()) Try it Yourself »
Videos
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 ...
07:51
Python Pandas Tail Method | dataframe.tail() | series.tail ...
Five important basic Pandas DataFrame functions info(), head(), ...
03:14
Python Basics Pandas DataFrame Tail Method - YouTube
Codecademy
codecademy.com › docs › python:pandas › dataframe › .tail()
Python:Pandas | DataFrame | .tail() | Codecademy
July 8, 2024 - In Pandas, .tail() is a method that returns the last n rows of a DataFrame. By default, it returns the last 5 rows, but the number of rows can be adjusted by passing an integer argument to the method.
PYnative
pynative.com › home › python › pandas › pandas dataframe head, tail, at, iat
Pandas DataFrame head, tail, at, iat
March 9, 2023 - DataFrame.tail(n=5)Code language: Python (python) Example · 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 ·
TutorialsPoint
tutorialspoint.com › article › python-pandas-how-to-use-pandas-dataframe-tail-function
Python Pandas – How to use Pandas DataFrame tail( ) function
May 27, 2024 - The Pandas DataFrame tail() function returns the last n rows of a DataFrame. This is particularly useful when combined with filtering operations to examine the bottom portion of your filtered data.
w3resource
w3resource.com › pandas › dataframe › dataframe-tail.php
Pandas DataFrame: tail() function - w3resource
August 19, 2022 - Pandas DataFrame - tail() function: The tail() function is used to return the last n rows.
w3resource
w3resource.com › pandas › series › series-tail.php
Pandas Series: tail() function - w3resource
Python-Pandas Code: import numpy as np import pandas as pd df = pd.DataFrame({'animal':['dog', 'bee', 'cat', 'lion', 'monkey', 'tiger', 'fox', 'wolf', 'zebra']}) df.tail() Output: animal 4 monkey 5 tiger 6 fox 7 wolf 8 zebra · Example - Viewing the last n lines (three in this case): Python-Pandas Code: import numpy as np import pandas as pd df = pd.DataFrame({'animal':['dog', 'bee', 'cat', 'lion', 'monkey', 'tiger', 'fox', 'wolf', 'zebra']}) df.tail(3) Output: animal 6 fox 7 wolf 8 zebra ·
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.tail.html
pyspark.pandas.DataFrame.tail — PySpark 4.1.1 documentation
>>> df.tail() animal 4 monkey 5 parrot 6 shark 7 whale 8 zebra · Viewing the last n lines (three in this case) >>> df.tail(3) animal 6 shark 7 whale 8 zebra · For negative values of n · >>> df.tail(-3) animal 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra ·
AlphaCodingSkills
alphacodingskills.com › pandas › notes › pandas-function-dataframe-tail.php
Pandas DataFrame - tail() function - AlphaCodingSkills
The Pandas DataFrame tail() function returns the last n rows of the dataframe. The syntax for using this function is given below. Syntax: DataFrame.tail(n)
Pandas
pandas.pydata.org › pandas-docs › version › 0.23.4 › generated › pandas.DataFrame.tail.html
pandas.DataFrame.tail — pandas 0.23.4 documentation
DataFrame.tail(n=5)[source]¶ · Return the last n rows. This function returns last n rows from the object based on position. It is useful for quickly verifying data, for example, after sorting or appending rows. See also · pandas.DataFrame.head · The first n rows of the caller object.
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]
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.tail.html
pandas.DataFrame.tail — pandas 3.0.1 documentation
>>> df.tail() animal 4 monkey 5 parrot 6 shark 7 whale 8 zebra · Viewing the last n lines (three in this case) >>> df.tail(3) animal 6 shark 7 whale 8 zebra · For negative values of n · >>> df.tail(-3) animal 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra ·
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › reference › api › pandas.DataFrame.tail.html
pandas.DataFrame.tail — pandas 2.1.4 documentation
>>> df.tail() animal 4 monkey 5 parrot 6 shark 7 whale 8 zebra · Viewing the last n lines (three in this case) >>> df.tail(3) animal 6 shark 7 whale 8 zebra · For negative values of n · >>> df.tail(-3) animal 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra ·
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.sql › api › pyspark.sql.DataFrame.tail.html
pyspark.sql.DataFrame.tail — PySpark 4.1.1 documentation
Running tail requires moving data into the application’s driver process, and doing so with a very large num can crash the driver process with OutOfMemoryError. New in version 3.0.0. Changed in version 3.4.0: Supports Spark Connect. Parameters · numint · Number of records to return. Will return this number of records or all records if the DataFrame contains less than this number of records. Returns · list · List of rows · Examples · >>> df = spark.createDataFrame( ...
Top answer 1 of 4
4
DataFrame.drop removes rows based on labels (the actual values of the indices). While it is possible to do with df1.drop(df1.index[-1]) this is problematic with a duplicated index. The last row can be selected with iloc, or a single value with .iat
if df1['number'].iat[-1] == 1:
df1 = df1.iloc[:-1, :]
2 of 4
2
You can check if the value of number in the last row is equal to one:
check = df1['number'].tail(1).values == 1
# Or check entire row with
# check = 1 in df1.tail(1).values
If that condition holds, you can select all rows, except the last one and assign back to df1:
if check:
df1 = df1.iloc[:-1, :]