JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas DataFrame.resample() Function

Author:JIYIK Last Updated:2025/05/01 Views:

Python Pandas DataFrame.resample() function resamples time series data.


pandas.DataFrame.resample()Syntax

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

rule It is an offset string or object representing the target transformation.
axis It specifies which axis to use for up or down sampling. For Series, the default is 0, i.e. along the rows.
closed It specifies which side of the bin interval is closed. It has two options: rightor left.
label It specifies the edge labels to binlabel bin. It has two options: rightor left.
convention It has four options: start, end, sor e, and for PeriodIndex only, it uses startor end.
kind It specifies the kind of index generated, with two options: Timestamp or Period. It has two options: timestampor period. timestampconverts the generated index to a DateTimeIndex, while periodconverts it to a PeriodIndex.
loffset It adjusts the time stamps for resampling.
base It is an integer. Its default value is 0.
on It indicates the name of the column to be used for resampling instead of the index. The column must be datetime-like.
level It indicates the level name to use for resampling. Levels must be date-like.
origin It is used to adjust the timestamp of the packet. It has three options: epoch, startor start_day.
offset It represents originthe offset to be added to the parameter timedelta.

Return Value

It returns the resampled object.


Example code: DataFrame.resample()Method to resample series data by week

import pandas as pd

index = pd.date_range('1/1/2021', periods=30, freq='D')
series = pd.Series(range(30), index=index)
print("The Original Series is: \n")
print(series)

series1= series.resample('W').sum()
print("The Resampled Data is: \n")
print(series1)

Output:

The Original Series is: 

2021-01-01     0
2021-01-02     1
2021-01-03     2
2021-01-04     3
2021-01-05     4
2021-01-06     5
2021-01-07     6
2021-01-08     7
2021-01-09     8
2021-01-10     9
2021-01-11    10
2021-01-12    11
2021-01-13    12
2021-01-14    13
2021-01-15    14
2021-01-16    15
2021-01-17    16
2021-01-18    17
2021-01-19    18
2021-01-20    19
2021-01-21    20
2021-01-22    21
2021-01-23    22
2021-01-24    23
2021-01-25    24
2021-01-26    25
2021-01-27    26
2021-01-28    27
2021-01-29    28
2021-01-30    29
Freq: D, dtype: int64
The Resampled Data is: 

2021-01-03      3
2021-01-10     42
2021-01-17     91
2021-01-24    140
2021-01-31    159
Freq: W-SUN, dtype: int64

The function returns the sum of the resamples in weekly units.


Example code: DataFrame.resample()Method to resample Seriesdata by month

import pandas as pd

index = pd.date_range('1/1/2021', periods=30, freq='D')
series = pd.Series(range(30), index=index)
print("The Original Series is: \n")
print(series)

series1= series.resample('M').sum()
print("The Resampled Data is: \n")
print(series1)

Output:

The Original Series is: 

2021-01-01     0
2021-01-02     1
2021-01-03     2
2021-01-04     3
2021-01-05     4
2021-01-06     5
2021-01-07     6
2021-01-08     7
2021-01-09     8
2021-01-10     9
2021-01-11    10
2021-01-12    11
2021-01-13    12
2021-01-14    13
2021-01-15    14
2021-01-16    15
2021-01-17    16
2021-01-18    17
2021-01-19    18
2021-01-20    19
2021-01-21    20
2021-01-22    21
2021-01-23    22
2021-01-24    23
2021-01-25    24
2021-01-26    25
2021-01-27    26
2021-01-28    27
2021-01-29    28
2021-01-30    29
Freq: D, dtype: int64
The Resampled Data is: 

2021-01-31    435
Freq: M, dtype: int64

The function returns the sum resampled by month.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Pandas DataFrame.insert() Function

Publish Date:2025/05/01 Views:116 Category:Python

Python Pandas DataFrame.insert() function inserts a column at the specified position into the DataFrame. pandas.DataFrame.insert() Syntax DataFrame . insert(loc, column, value, allow_duplicates = False ) parameter loc It is an integer param

Pandas DataFrame.idxmax() Function

Publish Date:2025/05/01 Views:79 Category:Python

Python Pandas DataFrame.idxmax() function returns the index of the maximum value in a row or column. pandas.DataFrame.idxmax() Syntax DataFrame . idxmax(axis = 0 , skipna = True ) parameter axis It is a parameter of integer or string type.

Pandas DataFrame sort_index() Function

Publish Date:2025/05/01 Views:183 Category:Python

This tutorial explains how to use pandas.DataFrame.sort_index() the sort method to sort a Pandas DataFrame based on its index. We will use the DataFrame shown in the above example to explain how to sort a Pandas DataFrame based on the index

Pandas cut function

Publish Date:2025/05/01 Views:165 Category:Python

pandas.cut() The function can distribute the given data into a range, which can also be called bins . We will use the following DataFrame in this article. import pandas as pd df = pd . DataFrame( { "Name" : [ "Anish" , "Birat" , "Chirag" ,

Appending to an Empty DataFrame in Pandas

Publish Date:2025/05/01 Views:54 Category:Python

As we learned earlier, Pandas in Python is an open source module that we can use for data analysis and making machine learning models. It is Numpy used along with another package called as they go hand in hand to support multidimensional ar

Pandas DataFrame DataFrame.query() function

Publish Date:2025/04/30 Views:108 Category:Python

The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e

Pandas DataFrame DataFrame.min() function

Publish Date:2025/04/30 Views:162 Category:Python

Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa

Pandas DataFrame DataFrame.mean() function

Publish Date:2025/04/30 Views:86 Category:Python

Python Pandas DataFrame.mean() function calculates the mean of the values ​​of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial