Pandas has statistical data by week, month, year, and year
The first step
Converts the date to a time format and sets it to an index
import pandas as pd
data=pd.read_excel('5\TB201812.xls',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)
Copy the code
Statistical data by week, month, quarter and year
import pandas as pd
data=pd.read_excel('5\TB201812.xls',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(data.resample('w').sum())
print(data.resample('m').sum())
print(data.resample('Q').sum())
print(data.resample('AS').sum())
Copy the code
Optimize using the to_period() method
Show data by month, quarter and year (no statistics)
import pandas as pd
data=pd.read_excel('5\TB201812.xls',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(data.resample('w').sum().to_period('w'))
print(data.resample('m').sum().to_period('m'))
print(data.resample('q').sum().to_period('q'))
print(data.resample('as').sum().to_period('a'))
Copy the code
The date display has changed from before