需求描述:
现在有一条生产线在生产一种产品,生产线上不同生产参数的数值会在许许多多的时间点上被记录,产品被生产出来时会有相对应的生产时间。对于产品A,小于其生产时间、并且离其生产时间最近的各个被记录的生产参数就是产品A所对应的生产参数。
提供生产表:parameter.xlsx 其中包含各个时间点、记录的参数名称以及参数值
提供产品表:ID_concat.xlsx 其中包含各个产品的生产时间
使用ID[y][i] = stove['value'][j]
的形式来更改DataFrame类型的某一个具体值,其中y
是列名,i
是行数。
ID.iloc[i][n]
用于定位某一个具体值,其中i
是行数,n
是列数。
import numpy as np
import pandas as pd
from datetime import *
stove_file = 'parameter.xlsx'
id_file = 'ID_concat.xlsx'
def find_value_from_stove(stove_file, id_file):
"""
stove_file: 按照时间先后顺序排序之后的参数文件
id_file: 按照时间先后顺序排序之后的Product_ID文件
return:DataFrame类型的、带有生产参数的Product数据
"""
ID = pd.read_excel(id_file) # sorted
#print(ID)
stove = pd.read_excel(stove_file) # sorted
index = 0
for i in range(ID.shape[0]):
print(i)
#print(len(ID))
#datetime.strptime("2016-06-07", "%Y-%m-%d")
end_time = int(ID['ts'][i].strftime('%Y%m%d%H%M%S'))
for j in range(index, len(stove)):
print(i,j)
now_time = int(stove['ts'][j].strftime('%Y%m%d%H%M%S'))
#print(end_time, now_time)
if(now_time<end_time):
y= stove['tagname'][j]
#print(y)
for n, k in enumerate(ID):
if y == k:#找到相同的参数列
#print(y,k)
print(ID.iloc[i][n], stove['value'][j])#修改之前的值
ID[y][i] = stove['value'][j]
print(ID.iloc[i][n], stove['value'][j])#修改之后的值
else:
index = j#更改查询范围
print(index)
if(i<ID.shape[0]-1):
for m, n in enumerate(ID):
if m>2:#在该文件中第四列以及之后才是参数列
ID[n][i+1] = ID.iloc[i][m]
break
return ID
ID = find_value_from_stove(stove_file, id_file)
ID = ID.to_excel("product_with_values.xlsx",index=0)