SoFunction
Updated on 2024-11-20

Introduction to Pandas statistics by week/month/year

Pandas statistics by week, month, year, stats

present (sb for a job etc)

Convert date to time format and set as an index

import pandas as pd
data=pd.read_excel('5\',usecols=['Order creation time','Total amount'])
print(data)
data['Order creation time']=pd.to_datetime(data['Order creation time'])
data=data.set_index('Order creation time')
print(data)

image-20211212113513921

Statistics by week, month, quarter, year

import pandas as pd
data=pd.read_excel('5\',usecols=['Order creation time','Total amount'])
data['Order creation time']=pd.to_datetime(data['Order creation time'])
data=data.set_index('Order creation time')
print(('w').sum())
print(('m').sum())
print(('Q').sum())
print(('AS').sum())

image-20211212113905454

image-20211212113915052

Using the to_period() method Optimization

Display of data by month, quarter and year (no statistics)

import pandas as pd
data=pd.read_excel('5\',usecols=['Order creation time','Total amount'])
data['Order creation time']=pd.to_datetime(data['Order creation time'])
data=data.set_index('Order creation time')
print(('w').sum().to_period('w'))
print(('m').sum().to_period('m'))
print(('q').sum().to_period('q'))
print(('as').sum().to_period('a'))

image-20211212114219970

image-20211212114235410

The way dates are displayed has changed compared to the previous one.

This article on Pandas weekly/monthly/yearly statistics is introduced to this article, more related Pandas statistics content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!