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.
π‘ Note: The pytz
comes packaged with pandas and does not require installation. However, this library is needed for the tz_ localize()
and tz_convert()
methods to work.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
If the installation was 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 import pytz
DataFrame resample()
The resample()
method is useful for manipulating the frequency and time-series data.
This DataFrame/Series must contain a datetime
-like index, for example:
DatetimeIndex
,PeriodIndex
,TimedeltaIndex
, or- the class must pass a date-like series/index to the
on
/level
keyword parameter.
The syntax for this method is as follows:
DataFrame.resample(rule, axis=0, closed=None, label=None, convention='start', kind=None, loffset=None, base=None, on=None, level=None, origin='start_day', offset=None)
Parameter | Description |
---|---|
rule | This parameter is the offset (string/object) representing a target conversion. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
closed | This parameter determines which side of the bin interval is closed. Default 'left' for all frequency offsets except: – 'M', 'A', 'Q', 'BM', 'BA', 'BQ' , and 'W' , default 'right' . |
label | This parameter determines which bin edge to label bucket. Default 'left' for all frequency offsets except: – 'Q', 'BM', 'BA', 'BQ' , and 'W' , default 'right' . |
convention | This parameter is the PeriodIndex , and it controls whether to use the start/end of the rule. The available options are: 'start' , 'end' , 's' , or 'e' . Default is 'start' . |
kind | This parameter is a timestamp/period and is for the PeriodIndex . |
loffset | Not in use since v1.1.0. Add this to df.index after resample() has taken place. |
base | Not in use since v1.1.0. Use 'offset' or 'origin' instead. |
on | If a DataFrame, the datetime column to use instead of index for resampling. |
level | A datetime level in a MultiIndex scenario to use for resampling. |
origin | The timestamp to adjust the grouping. The origin time-zone must match the index. If a string, one of the following: 'epoch' , 'start' , 'start_day' , 'end' , and 'end_day' |
offset | This parameter is the offset timedelta which adds to the origin. |
Rivers Clothing is having a 3-hour blow-out sale for a new line they have introduced, scarfs. This example resamples the sales data and adds up the total number of scarf sales per hour.
df = pd.read_csv('rivers.csv', parse_dates=['date'], index_col=['date']) print(df) result = df.resample('1H').sum() print(result)
- Line [1] reads in a CSV file, parses the date column, and sets this column as the index. The output saves to
df
. - Line [2] outputs the DataFrame to the terminal.
- Line [3] resamples the data by grouping the total scarf sales by the hour. The output saves to
result
. - Line [4] outputs the result to the terminal.
Output
df
Item | color | sold | |
date | |||
2022-01-27 08:17:00 | scarf | red | 3 |
2022-01-27 08:23:00 | scarf | blue | 2 |
2022-01-27 08:47:00 | scarf | pink | 1 |
2022-01-27 09:01:00 | scarf | black | 11 |
2022-01-27 09:28:00 | scarf | brown | 6 |
2022-01-27 09:51:00 | scarf | burgundy | 15 |
2022-01-27 10:11:00 | scarf | black | 21 |
2022-01-27 10:13:00 | scarf | brown | 10 |
2022-01-27 10:22:00 | scarf | black | 9 |
2022-01-27 10:28:00 | scarf | navy | 30 |
result
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.