要求:
数据:
代码:计数
import pandas as pd
import datetime
while True:
try:
date_start = input('请输入起始日期(如:2019-02-03):') #输入起止日期及间隔时间
date_end = input('请输入截止日期(如:2019-02-03):')
periods = int(input('间隔时间(具体数字):'))
date_start_datetime = datetime.datetime.strptime(date_start, '%Y-%m-%d').strftime('%Y-%m-%d')
date_end_datetime = datetime.datetime.strptime(date_end, '%Y-%m-%d').strftime('%Y-%m-%d')
data_range = pd.date_range(date_start_datetime, date_end_datetime)
for i in data_range: #计算每天的需求
end = i.strftime('%Y-%m-%d')
start = (i - datetime.timedelta(days=periods)).strftime('%Y-%m-%d')
file_path = 'need.xlsx' # 文件路径
data = pd.read_excel(file_path, usecols=[1, 2, 3, 4, 5, 6, 7]) # 选择数据并筛选
data = data[(data['RECEIVE_PAY_CD'] == 2) & (data['OVERAREA_IND'] == 1) & (pd.to_datetime(data['TX_DT']) > start) & (
pd.to_datetime(data['TX_DT']) <= end)]
data2 = pd.pivot_table(data, index='CUST_ID', values='CNY_AMT', aggfunc=[len])
data2['date'] = end
data2.to_csv('result_1.csv', mode='a') #写入数据
break
except:
print('间隔时间请输入具体数字,起止日期请输入如“ 2019-02-03 ”格式的数字')
代码二:累计金额
import pandas as pd
import numpy as np
import datetime
while True:
try:
date_start = input('请输入起始日期(如:2019-02-03):') #输入起止日期及间隔时间
date_end = input('请输入截止日期(如:2019-02-03):')
periods = int(input('间隔时间(具体数字):'))
date_start_datetime = datetime.datetime.strptime(date_start, '%Y-%m-%d').strftime('%Y-%m-%d')
date_end_datetime = datetime.datetime.strptime(date_end, '%Y-%m-%d').strftime('%Y-%m-%d')
data_range = pd.date_range(date_start_datetime, date_end_datetime)
for i in data_range: #计算每天的需求
end = i.strftime('%Y-%m-%d')
start = (i - datetime.timedelta(days=periods)).strftime('%Y-%m-%d')
file_path = 'need.xlsx' # 文件路径
data = pd.read_excel(file_path, usecols=[1, 2, 3, 4, 5, 6, 7]) # 选择数据并筛选
data = data[(data['RECEIVE_PAY_CD'] == 2) & (data['OVERAREA_IND'] == 1) & (pd.to_datetime(data['TX_DT']) > start) & (data['CUST_CLASS_CD'] == 'C') & (
data['OPP_CUST_CLASS_CD'] == 1) & (pd.to_datetime(data['TX_DT']) <= end)]
data2 = pd.pivot_table(data, index='CUST_ID', values='CNY_AMT', aggfunc=[np.sum])
data2['date'] = end
data2.to_csv('result.csv', mode='a') #写入数据
break
except:
print('间隔时间请输入具体数字,起止日期请输入如“ 2019-02-03 ”格式的数字')