By using iloc[-1] property and tail(1) you can get the last row of Pandas.DataFrame. iloc[] is a property that is used to select rows and columns by position/index. If the position/index does not exist, it gives an index error. In this article, I will cover the usage of pandas.DataFrame.iloc[] and using this how we can get the last row of Pandas DataFrame in different ways with examples.
pandas loc[] is another property that is used to operate on the column and row labels. For a better understanding of these two learn the differences and similarities between pandas loc[] vs iloc[].
1. Quick Examples to Get the Last Row of DataFrame
If you are in a hurry, below are some quick examples of how to get the last row of Pandas DataFrame.
# Below are quick example
# Example 1: Get last row using row position
print(df.iloc[-1])
# Example 2: Get the last row use tail()
print(df.tail(1))
# Example 3: Get last row using range index
print(df.iloc[-1:])
# Example 4: Get last row value using particular column
print(df['Fee'].iloc[-1])
# Example 5: Get last row value using index range
print(df['Discount'].iloc[:-1])
# Example 6: Get last row using loc() function
print(df.loc[df.index[-1]])
# Example 7: Get last row using values[]
print(df.values[-1:])
# Example 8: Get last row of particular column
print(df['Fee'].values[-1:])
# Example 9: Get the last row of DataFrame as a list
print(df.iloc[-1].tolist())
Let’s create DataFrame using data from the Python dictionary then use the above examples to get the first row of DataFrame.
# Import pandas library
# Create pandas DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
'Fee' :[20000,25000,26000,22000,24000],
'Duration':['30day','40days','35days','40days','60days'],
'Discount':[1000,2300,1200,2500,2000]
}
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies, columns = ['Courses', 'Fee', 'Duration', 'Discount'], index = index_labels)
print(df)
# Outputs
Courses Fee Duration Discount
r1 Spark 20000 30day 1000
r2 PySpark 25000 40days 2300
r3 Hadoop 26000 35days 1200
r4 Python 22000 40days 2500
r5 pandas 24000 60days 2000
2. Get the Last Row of Pandas using iloc[]
Using the Pandas iloc[-1]
attribute you can select the last row of the DataFrame. iloc[] is used to select the single row or column by using an index. iloc[-1] property return the last row of DataFrame in the form of Pandas Series.
# Get the last row using row position
print(df.iloc[-1])
# Output:
# Courses pandas
# Fee 24000
# Duration 60days
# Discount 2000
Name: r5, dtype: object
We can also get the last row of Pandas DataFrame by providing an index range i.e.[-1:]
to iloc[]
attribute. This syntax will be returned the last in the form of DataFrame. For example,
# Get last row using range index
print(df.iloc[-1:])
# Output:
# Courses Fee Duration Discount
# r5 pandas 24000 60days 2000
3. Get the First Row for a Particular Column
If you want to get the last row based on a particular column, we can pass the specified column into DataFrame and then call iloc[] attribute, it will return the last row based on the specified column.
# Get last row value using particular column
print(df['Fee'].iloc[-1])
# Output:
# 24000
Alternatively, we can also get the last row based on a particular column using the index range of the iloc[] attribute. It will return the last row value in the form of a Series.
# Get last row value using index range
print(df['Discount'].iloc[:-1])
# Output:
# r5 2000
# Name: Discount, dtype: int64
4. Get the Last Row using loc()
We can also get the last row of DataFrame using the loc[] attribute for that, we have to pass the last row index with the help of the index[] attribute. It will return the last row in the form of Series object.
# Get last row using loc() function
print(df.loc[df.index[-1]])
# Output:
# Courses pandas
# Fee 24000
# Duration 60days
# Discount 2000
# Name: r5, dtype: object
5. Get the Last Row of Pandas using values[]
Pandas DataFrame.values
attribute is used to return a Numpy representation of the given DataFrame. Use this attribute to get the last row of DataFrame in the form of NumPy array.
# Get last row using values[]
print(df.values[-1:])
# Output:
# [['pandas' 24000 '60days' 2000]]
# Get last row of particular column
print(df['Fee'].values[-1:])
# Output:
# [24000]
6. Get Last Row of DataFrame using tail()
The pandas.DataFrame.tail()
method returns the last n rows of dataframe. We can use this tail() function to get only the last row of the dataframe, for that, we pass '1'
as an argument to this function. It will return the last row of DataFrame.
# Get the last row use head()
print(df.tail(1))
# Output:
# Courses Fee Duration Discount
# r5 pandas 24000 60days 2000
7. Get the Last Row of pandas as List
As we know from the above, we have got the last row of dataframe using df.iloc[-1]. It has given the result as a series object and then using the series.tolist() function, we can get the last row of the dataframe in the form of a list. For example,
# Get the last row of DataFrame as a list
print(df.iloc[-1].tolist())
# Output:
# ['pandas', 24000, '60days', 2000]
Conclusion
In this article, I have explained the usage of DataFrame.iloc[] and using this how we can get the last row of DataFrame in different ways. As well as I explained how to get the last row using tail() and other functions.
Happy Learning !!