Preparation
Before any data manipulation can occur, one (1) new library will require installation:
- The Pandas library enables access to/from a DataFrame.
To install this library, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
If the installations were successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd
π‘ Note: To follow along with the examples below, click here to download the finxters.csv
file of auto-generated dummy user data. Move this file to the current working directory.
DataFrame last()
The last()
method retrieves and returns the last set number of rows (periods) based on the value entered. The index must be a date value for this method to return the expected results.
The syntax for this method is as follows:
DataFrame.last(offset)
Parameter | Description |
---|---|
offset | This parameter is the date period of the data to display (ex: 1M, 2D). |
For this example, the blood pressure for three (3) patients over a two (2) month period is retrieved.
r = pd.date_range('2021-01-01', periods=3, freq='1M') df = pd.DataFrame({'Patient-1': [123, 120, 144], 'Patient-2': [129, 125, 90], 'Patient-3': [101, 95, 124]},index=r) result = df.last('1M') print(result)
- Line [1] sets up the following:
- The
date
range start date ('2021-01-01'
). - The number of periods (
3
). - The frequency (
'1M'
). This statement equates to 1 Month.
- The
- Line [2] creates a DataFrame containing:
- Three (3) patient names containing three (3) elements of data for each patient.
- Line [3] saves the output to
result
. - Line [4] outputs the result to the terminal.
Output
Patient-1 | Patient-2 | Patient-3 | |
2022-03-31 | 144 | 90 | 125 |
π‘ Note: The date range for the selected frequency references the last day of the month.
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.