Adventures in Machine Learning

Mastering Pandas: 2 Easy Ways to Get the Last Row

Pandas is a powerful data manipulation tool in Python, and it provides a variety of methods to filter, sort, group, reshape, and transform data. In this article, we are going to explore one of the common data manipulation tasks in Pandas, i.e., getting the last row of a DataFrame.

We will discuss two different methods to get the last row, one as a Pandas Series and the other as a

Pandas DataFrame.

Method 1: Get the Last Row as a Pandas Series

The first method to get the last row of a

Pandas DataFrame is by using the ‘iloc’ property.

The ‘iloc’ property is used to access rows and columns of a DataFrame by integer position. Here’s how you can retrieve the last row of a

Pandas DataFrame as a Pandas Series:

“`python

import pandas as pd

data = {‘Name’: [‘Tom’, ‘John’, ‘Smith’, ‘Lisa’],

‘Age’: [23, 35, 28, 29],

‘Country’: [‘US’, ‘UK’, ‘US’, ‘Canada’]}

df = pd.DataFrame(data)

last_row_series = df.iloc[-1]

“`

In the above code, we create a sample

Pandas DataFrame with some random data. To get the last row as a Pandas Series, we use the ‘iloc’ property and pass ‘-1’ as the index.

The ‘-1’ index indicates the last row of the DataFrame. Finally, we store the result in a variable ‘last_row_series’.

We can print the ‘last_row_series’ variable to see the output:

“`python

print(last_row_series)

“`

Output:

“`

Name Lisa

Age 29

Country Canada

Name: 3, dtype: object

“`

As you can see, the output is a Pandas Series that contains the values of the last row in the DataFrame. The index of the series shows the column names, and the values correspond to the values in the last row.

Method 2: Get the Last Row as a

Pandas DataFrame

The second method to get the last row of a

Pandas DataFrame is by using the same ‘iloc’ property, but this time, we use it with double brackets to get the last row as a DataFrame rather than a Series. Here’s how you can do it:

“`python

import pandas as pd

data = {‘Name’: [‘Tom’, ‘John’, ‘Smith’, ‘Lisa’],

‘Age’: [23, 35, 28, 29],

‘Country’: [‘US’, ‘UK’, ‘US’, ‘Canada’]}

df = pd.DataFrame(data)

last_row_df = df.iloc[[-1]]

“`

In the above code, we create a sample

Pandas DataFrame with the same data as before. To get the last row as a

Pandas DataFrame, we use the ‘iloc’ property but this time, we pass ‘[-1]’ as the index instead of just ‘-1’.

The double brackets create a new DataFrame from the last row, and we store it in a variable ‘last_row_df’. We can print the ‘last_row_df’ variable to see the output:

“`python

print(last_row_df)

“`

Output:

“`

Name Age Country

3 Lisa 29 Canada

“`

As expected, the output is a DataFrame that contains the values of the last row in the DataFrame.

Examples of How to Use Methods

Example 1: Get Last Row as a Pandas Series

Let’s say we have a

Pandas DataFrame that contains the sales data of a company for the past year. We want to retrieve the sales figure of the last month as a Pandas Series.

Here’s how we can do it:

“`python

import pandas as pd

data = {‘Month’: [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’],

‘Sales’: [5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000, 16000]}

df = pd.DataFrame(data)

last_month_sales = df.iloc[-1][‘Sales’]

print(f”The sales figure for the last month is {last_month_sales}”)

“`

Output:

“`

The sales figure for the last month is 16000

“`

In the above code, we create a

Pandas DataFrame that contains the sales data for the past year. To get the sales figure of the last month, we use the ‘iloc’ property with ‘-1’ as the index to get the last row as a Pandas Series, and then we retrieve the value of the ‘Sales’ column from the series.

Finally, we print the sales figure for the last month using an f-string. Example 2: Get Last Row as a

Pandas DataFrame

Let’s say we have a

Pandas DataFrame that contains the grades of a class in a subject.

We want to retrieve the grades of the last three students as a

Pandas DataFrame. Here’s how we can do it:

“`python

import pandas as pd

data = {‘Student’: [‘Tom’, ‘John’, ‘Smith’, ‘Lisa’, ‘Mary’, ‘Bob’, ‘Emma’, ‘David’, ‘Sophie’, ‘Chris’],

‘Grade’: [80, 85, 90, 95, 88, 93, 87, 91, 89, 92]}

df = pd.DataFrame(data)

last_three_grades_df = df.iloc[[-3, -2, -1]]

print(last_three_grades_df)

“`

Output:

“`

Student Grade

7 David 91

8 Sophie 89

9 Chris 92

“`

In the above code, we create a

Pandas DataFrame that contains the grades of a class in a subject. To get the grades of the last three students, we use the ‘iloc’ property with ‘[-3, -2, -1]’ as the index to get a new DataFrame from the last three rows.

Finally, we print the ‘last_three_grades_df’ variable to see the output. Conclusion:

In this article, we have discussed two different methods to get the last row of a

Pandas DataFrame, one as a Pandas Series and the other as a

Pandas DataFrame.

We have also provided two examples to demonstrate how these methods can be used in practice. These methods are easy to implement and can be useful when dealing with large datasets that require specific data manipulation tasks.

In this expansion, we will explore some additional resources that can help you learn more about

Pandas DataFrames and common data manipulation tasks in Pandas. Pandas is a popular library for data analysis and is widely used in various domains such as finance, healthcare, marketing, and social sciences.

It provides numerous functions and methods to perform data manipulation tasks efficiently and quickly.

Pandas DataFrame

A

Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is similar to a spreadsheet or a SQL table.

You can think of a DataFrame as a collection of Series that share the same index. You can perform various operations on DataFrames, such as filtering, grouping, sorting, merging, pivoting, and reshaping.

Common Tasks in Pandas

Pandas provides a plethora of functions and methods to perform common data manipulation tasks. Some of the most commonly used tasks include selecting rows and columns, filtering rows based on conditions, sorting rows, grouping data, and merging data from multiple DataFrames.

Tutorials

If you are new to Pandas or want to learn more about it, there are many online resources that can help you get started. Here are some of the most popular tutorials for Pandas:

1.

Pandas documentation – The official documentation of Pandas is one of the most comprehensive resources for learning Pandas. It covers all the functions and methods of Pandas and provides many examples and use cases.

The documentation is available online and can be downloaded as a PDF. 2.

Pandas for Data Analysis – This is a book by Wes McKinney, the creator of Pandas. The book covers all the essential topics of Pandas, such as loading data, cleaning data, manipulating data, and visualizing data.

The book also includes many examples and use cases to help you learn Pandas in a practical way. 3.

Kaggle

Tutorials – Kaggle is a platform for data science competitions and provides many tutorials and courses on various data science topics, including Pandas. The Pandas tutorial on Kaggle covers all the essential topics of Pandas, such as selecting data, filtering data, sorting data, and merging data.

4. DataCamp – DataCamp is an online platform for learning data science, and it provides many courses on various topics, including Pandas.

The Pandas course on DataCamp covers all the essential topics of Pandas, such as manipulating data, cleaning data, grouping data, and merging data. The course includes many interactive exercises and projects to help you learn Pandas in a practical way.

In conclusion, Pandas is a powerful library for data analysis, and DataFrames are one of its most vital components. Understanding DataFrames can help you perform various data manipulation tasks efficiently and quickly.

There are many resources available to help you learn Pandas and DataFrames, such as online courses, books, and documentation. Whether you are a beginner or an advanced user, there are plenty of resources to help you become proficient in Pandas.

In summary,

Pandas DataFrames are a vital component in data analysis, and knowing how to perform common data manipulation tasks on DataFrames is crucial for efficient and accurate data analysis. We have discussed two methods for retrieving the last row of a

Pandas DataFrame, one as a Pandas Series and the other as a

Pandas DataFrame.

Additionally, we have provided some resources, such as tutorials and documentation, that can help you learn more about Pandas and DataFrames. Whether you are a beginner or an advanced user, these resources can help you become proficient in Pandas and DataFrames.

By leveraging these tools and techniques, you can unlock the full power of Pandas and gain deeper insights from your data.

Popular Posts