问题描述:
表1中有两列数据分别是开始时间和结束时间,表2中有两列数据分别是参数1和参数2,需求是通过result = he * Start + hst * End求出每一行数据的答案。
求解步骤:
分别读取两个csv文件,并从中按行取出相应的四个参数,作完运算后再存入空列表中,导入到新csv文件中。
import numpy as np
import pandas as pd
from sympy import *
#数据表格
df = pd.read_csv(r'C:/Users/hanhan/PycharmProjects/pythonProject/1.csv',encoding = 'gbk')
df1 = pd.read_csv(r'C:/Users/hanhan/PycharmProjects/pythonProject/2.csv',encoding = 'gbk')
#参数定义
result = []
for i in range(len(df)-1, 0,-1):
Start = np.array(df['开始时间'].iloc[i:i+q])
End = np.array(df['结束时间'].iloc[i:i+q])
he = np.array(df1['参数1'].iloc[i:i+q])
hst = np.array(df1['参数2'].iloc[i:i+q])
"""
result = he * Start + hst * End
"""
answer = he * Start + hst * End
result.append(answer[0])
ls = np.array([result])
print(ls.shape)
df = pd.DataFrame(ls.transpose())
df.to_csv(r'C:/Users/hanhan/PycharmProjects/pythonProject/result.csv', encoding='gbk')
还有就是如果是和表2中的参数随机搭配,可以先生成一个随机数,让参数1和参数2根据这个随机数取值。
j = random.randint(0,len(df1))
he = np.array(df1['0'].iloc[j:j + q])
hst = np.array(df1['1'].iloc[j:j + q])